Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 31st, 2007, 10:17 AM
Registered User
Join Date: Jul 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to swap the table column value

hi,i have to swap the column value of table using single sql query,anyone of u know abt how to do this then pls reply to this msg.
for ex- i have a table Name which has two column-firstname and last name.i want to swap firstname with lastname.how to do this?
thanks in advance

Old July 31st, 2007, 10:23 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi

hi there.. do you mean to do it in one query?? do you want to add new rows or update the same row??



================================================== =========
Read this if you want to know how to get a correct reply for your question:
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
================================================== =========
I know that CVS was evil, and now i got the proof:
================================================== =========
Old July 31st, 2007, 06:43 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post

Homework problem?

Assuming you want to simply switch the values of the two columns in each row, this will do it:

UPDATE Yourtable
SET FirstName = LastName,
      LastName = FirstName

(Some not familiar with the set based nature of SQL may wonder how this works; i.e. without an intermediate variable... ;))

Jeff Mason

Similar Threads
Thread Thread Starter Forum Replies Last Post
Swap Images Mike707 Dreamweaver (all versions) 3 December 18th, 2006 01:05 PM
swap image onclick The Beginner Javascript How-To 2 November 8th, 2006 08:42 AM
Problem to Swap two number in C# furqanms C# 1 June 5th, 2006 05:12 PM
How to Swap two integers without temp variable ramk_1978 C# 2 February 28th, 2006 08:03 PM
how to make column of table 1 = to column of table gilgalbiblewheel Classic ASP Databases 4 October 11th, 2004 11:57 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.