How to change 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 change the data from the AttributeValue column to AttributeName and AttributeName to AttributeValue

Example:

 Eno ename AttributeValue AttributeName 1 aa abc a123 2 bbb dcf b123 3 cc wew3 c7sd 
+42
sql sql-server sql-server-2008
Nov 16 '10 at 20:25
source share
6 answers
 UPDATE employee SET AttributeValue = AttributeName, AttributeName = AttributeValue 

However, if both columns are not precisely defined, you risk losing information.

+62
Nov 16 '10 at 20:26
source share
β€” -
 Update employee Set attributeValue = attributeName, attributeName = attributeValue 
+11
Nov 16 '10 at 20:26
source share

update Employee set AttributeValue = AttributeName, AttributeName = AttributeValue

+4
Nov 16 '10 at 20:26
source share

This is a really good example.

 SELECT * from employees; Go DECLARE @temp as varchar(20) update employees set @temp = fname, fname = lname, lname = @temp WHERE deptno = 10; GO SELECT * from employees; 

Result

+2
Oct 07 '14 at 1:47
source share
 Declare @myTable Table (id int, first_name varchar(50), last_name varchar(50)); Select * from Student Insert Into @myTable (id, first_name, last_name) Select id, last_name, first_name from Student MERGE INTO Student std USING @myTable tmp ON std.id = tmp.id WHEN MATCHED THEN UPDATE SET std.first_name = tmp.first_name, std.last_name = tmp.last_name; Select * from Student 

Exit

Query Result Screenshot

+1
Jun 01 '17 at 6:30
source share

Just swap both columns in one update:

 Update registration Set AttributeName = AttributeValue , AttributeValue = AttributeName where id in (1,2,3) 
0
Jul 09 '17 at 9:00
source share



All Articles