Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I swap column values in sql server 2008?

I have a table called Employee

 Eno     ename     AttributeValue      AttributeName    1       aa           a123             abc  2       bbb          b123             dcf  3       cc           c7sd             wew3 

I want to swap the data from column AttributeValue to AttributeName and AttributeName to AttributeValue

For Example:

Eno     ename     AttributeValue   AttributeName   1       aa        abc              a123 2       bbb       dcf              b123 3       cc        wew3             c7sd 
like image 770
jay Avatar asked Nov 16 '10 20:11

jay


People also ask

How do you interchange column values in SQL Server?

SET Col1 = Col2, Col2 = Col1; When you run above update statement, the values of the columns will be swapped in SQL Server. There is no need for temporary column, variable or storage location in SQL Server. You can validate that with the SELECT statement here.

How do I change the value of one column to another column?

In such a case, you can use the following UPDATE statement syntax to update column from one table, based on value of another table. UPDATE first_table, second_table SET first_table. column1 = second_table. column2 WHERE first_table.id = second_table.

How can I swap two numbers in SQL Server?

No need for multiple statements or variables, this can be done in a single statement: update emp set phone_number = case when 205 then (select phone_number from emp where employee_id = 209) when 209 then (select phone_number from emp where employee_id = 205) end where employee_id in (205, 209);


2 Answers

UPDATE employee SET AttributeValue = AttributeName,      AttributeName = AttributeValue 

However, unless both columns have the exact same definition, you risk losing information.

like image 179
Oded Avatar answered Oct 12 '22 23:10

Oded


Update employee Set attributeValue = attributeName,     attributeName = attributeValue 
like image 40
John Hartsock Avatar answered Oct 13 '22 00:10

John Hartsock