How to add new column b/w two existing columns in sql ?
How to add new column b/w two existing columns in sql ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
selva kumarPosted May 17, 2013, 9:33 AM
use these query
Alter Table
Shankar MPosted May 17, 2013, 6:53 AM
When you add a column to the Table using ALTER Table method, the column is usually added to the Last only..
And we don't have any direct method to use it ALTER Table method.
And this can be done something like this, Replacing the existing column name to another name and the new column that will be usually added to the Last.
Thanks, Shankar M
Hemant SrivastavaPosted May 12, 2013, 10:47 PM
Suppose you have initially columns like Col1 and Col3.
Now add the column 'Col2' using ALTER query (as above)
This will give you result like: Col1,Col3, Col2
Now I am saying, if you want to get result in the logical order as: Col1,Col2,Col3
you may use your select query like: Select Col1,Col2,Col3 From Table1 Where....
(However physical order of the columns are still 1,3,2 not 1,2,3 but you can get your work done by their Logical order)
You can always set logical order in your 'SELECT' query. If you really interested insert column physically in between two columns, for that I don't have idea and I think, it might not be possible in SQL with ALTER command.
Santosh YadavPosted May 12, 2013, 3:07 AM
Thank you very much for your kind of reply.
Actually this is not the right answer for my question.
My question is that suppose we have two columns in a table col1 and col3. Now we have to insert another column between those two columns.
Col1, Col3
Now add Col2 on the below position :
Col1, Col2, Col3.
Now what would be the appropriate Query ?
Hemant SrivastavaPosted May 11, 2013, 1:05 PM
You want to insert it in between two columns.. but why?
The fact is: Column order you can always set in the query. Right!
For example:
Select Col1, Col2, Col3 FROM Table
OR
SELECT Col1, Col3, Col2 FROM Table
Anyway ALTER query is:
ALTER
TABLE Your_Table
ADD NewlyAdded_Column varchar(50);
Hope this would help you. Let me know if you have any issue.