Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

Swap data between two columns in SQL SERVER

Experts,

I have a database table that looks like this:

RecordID     Code          Desc1        Desc2
1                CBY-100     Insert         Mill0.1
2                CBY-101     Insert         Mill1.3
3                CBY-102     Insert         Mill2.4
4                CBY-103     Insert         Mill3.2

I would like to swap out the descriptions so that it looks like this:

RecordID     Code          Desc1        Desc2
1                CBY-100     Mill0.1       Insert
2                CBY-101     Mill1.3       Insert
3                CBY-102     Mill2.4       Insert
4                CBY-103     Mill3.2       Insert

I know how to update one field but i can't seem to do both at the same time. As always any and all help is appreciated.

Raul.
0
Dakid24
Asked:
Dakid24
3 Solutions
 
m1tk4Commented:
1. Rename Desc2 to Desc3
2. Rename Desc1 to Desc2
3. Rename Desc3 to Desc1
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
Better to rename the column

exec sp_rename 'TableName.Desc1',Desc3,  'COLUMN'
exec sp_rename 'TableName.Desc2',Desc1,  'COLUMN'
exec sp_rename 'TableName.Desc3',Desc2,  'COLUMN'
0
 
LowfatspreadCommented:
update tablename
  set desc1 = desc2 , desc2 = desc1
 where desc1 <> desc2

should do it....




 
0
 
Dakid24Author Commented:
Thanks for the quick response. All tips worked. I wanted to split up the points three ways by 200 but it wouldn't let me. I took the UPDATE statement as my answer. Thanks for all the help again, you experts rock.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now