Wednesday 20 July 2016

How to Swap Values between Two Columns in SQL Server

swapping data

Description:

In this example we explain that how to Swap values between two columns using SQL Server query. Or how to update column1 values to column2 and column2 values to column1 like vice versa in sql server using update statement.

As we all know that when we working with database moving, swapping and updating values between a column is a common task for the all the developer in IT industries because there is a routine work.
Here in the below example we have one table called test in which there are two column column1 and column2 in which we have to swap the data column1 to column2 and vice versa.


Below is the sql query to swap the data between two columns in SQL Server.

Before Swapping :

SQL Server: How to Swap Values between Two Columns


DECLARE @temp as float
update Test
set    @temp = column1,
       column1 = column2,
       column2 = @temp

After Swapping :


SQL Server: How to Swap Values between Two Columns
This entry was posted in :

0 comments:

Post a Comment