How to interchange the values of two columns in sql


How to interchange the values of two columns in sql In this article I'm going to explain how to interchange column values in database table. In sql server there is no pre defined function for interchange column values.

How to interchange the values of two columns in sql
In this article I'm going to explain how to interchange column values in database table.
In sql server there is no pre defined function for interchange column values.

Example:

First we need to create simple table and then we need to insert some values to that table.
and then we are going to update that table. that's it.

	
Create Sample table:

Create table Sample(EmpID int, firstname nvarchar(100),lastname nvarchar(100))

Insert some values to Sample table:

Insert into Sample values (1,'nathan','rengan')
Insert into Sample values (2,'k','kumar')
Insert into Sample values (3,'Sachin','kumar')
Insert into Sample values (4,'Sachin2','kumar2')

Insert into Sample values (5,'Sachin4','kumar3')

Before Interchange the column values Records will display like this


1 nathan rengan
2 k kumar
3 Sachin kumar
4 Sachin2 kumar2
5 Sachin4 kumar3


This is very simple task, you need to write simple update query like this

update Sample set firstname =lastname ,lastname =firstname


After run above query the result will look like as follows

Select * from Sample


1 rengan nathan
2 kumar k
3 kumar Sachin
4 kumar2 Sachin2
5 kumar3 Sachin4


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: