Please enable Javascript to correctly display the contents on Dot Net Tricks!

Swap the values of two columns in SQL Server

 Print 
  Author : Shailendra Chauhan
Posted On : 07 Nov 2012
Total Views : 138,268   
Updated On : 26 Sep 2016
 

Do you have fun with SQL Server?. Let's start fun with SQL Server. Suppose you want to swap the values of two columns of a table in SQL Server, how could you achieve this tricky task?. Actually, it is simple and so funny.

Suppose you have a Customer table in the database with the following data and you want to interchange the values of columns Name and Address then how do you do?

 SELECT * FROM CUSTOMER 

Don't worry, to do this task, you need to just write a simple update query for Customer table like as :

 UPDATE Customer SET Name=Address , Address=Name 

Now After query execution you will find the the values of columns Name and Address have been interchanged, that's so cool.

 SELECT * FROM CUSTOMER 

Note

  1. Data type of both the columns should be same and also length should be enough to hold the swapped column data other wise data will be truncated.

What do you think?

I hope you will enjoy this trick while having fun with SQL Server. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS
24 SEP
Angular2 and Angular4 (Classroom)
08:30 AM-11:30 AM IST
23 SEP
MEAN Stack (Classroom)
8:30 Am To 11:30 Am
23 SEP
Microsoft Azure Infrastructure Solutions (Online)
08:00 PM-11:00 PM IST / 9:30 AM-12:30 PM CST
20 SEP
MEAN Stack (Online)
07:00 AM-09:00 AM IST
20 SEP
ASP.NET MVC with Angular4 (Online)
9:00PM- 11:00PM IST(+5:30GMT)
16 SEP
Angular2 and Angular4 (Online)
08:00 AM-10:00 AM IST(+5.30 GMT)
7 SEP
ASP.NET MVC with Angular4 (Online)
09:00 PM to 11:00 PM IST (+5:30 GMT)
22 AUG
ASP.NET Core with Angular4 (Online)
07:00 AM - 9:00 AM IST(+5:30 GMT)
19 AUG
MEAN Stack 2 (Online)
5:30 Pm - 7:00 PM IST (+5:30 GMT)
19 AUG
ASP.NET MVC with Angular4 (Online)
10:30 Am to 12:30 PM IST (+ 5:30 GMT)
19 AUG
NodeJS with Angular4 (Classroom)
04:00 PM to 07:00 PM IST (+5:30 GMT)
12 AUG
ASP.NET MVC with Angular4 (Classroom)
11:30 AM - 1:30 PM IST (+5:30GMT)
15 JUL
ASP.NET MVC with Angular4 (Online)
03:30 PM - 05:30 PM IST(+5.30 GMT)
LIKE US ON FACEBOOK
 
+