Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query to shift 3 columns Data over one place?

Status
Not open for further replies.

nstouffer

Programmer
Jan 11, 2002
52
US
Does anyone know a query that will shift the data in 3 columns over one place to the right.

Ex:

Seg1 Seg2 Seg3 Seg4
01 400300 700
01 400325 700

to

Seg1 Seg2 Seg3 Seg4
01 400300 700
01 400325 700


This is for large datasets...

thanks
 
Use an update query. It is fairly simple.

Update Yourtable With (tablockx holdlock)
Set Seg4=Seg3, Seg3=Seg2, Seg2=Seg1, Seg1=null

If the data set is large you should do the update in batches. Perhaps, you could use something like the following. Modify as needed for your process.

Set Rowcount 10000
Declare @rc int
Set @rc=1

While @rc>0
Begin

Begin Transaction

Update Yourtable With (tablockx holdlock)
Set Seg4=Seg3, Seg3=Seg2, Seg2=Seg1, Seg1=null
Where Seg1 Is Not Null

Set @rc=@@rowcount

Commit

End Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top