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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL to change where data is

Status
Not open for further replies.

kb178

IS-IT--Management
Aug 16, 2001
83
0
0
US
I have a table with a First Name column and a Last Name column. 1/2 the records are incorrect where the last names are in the first name column and the first names are in the last name column. I know it's certain records between 2 ids. Is there an easy way to switch the records?
Thanks,
Kristen
 
There are a number of ways of doing this but I would create two new fields in the table

CORRECT_FIRST_NAME
CORRECT_SURNAME

THEN

UPDATE MYTABLE
SET CORRECT_FIRST_NAME = FirstNameField --from your table
WHERE ID Between (range that you know the first name is in the correct field)

UPDATE MYTABLE
SET CORRECT_FIRST_NAME = SurnameNameField --from your table
WHERE ID Between (range that you know the first name is in the incorrect field)

UPDATE MYTABLE
SET CORRECT_SURNAME = FirstNameField --from your table
WHERE ID Between (range that you know the first name is in the incorrect field)

UPDATE MYTABLE
SET CORRECT_SURNAME = SurnameNameField --from your table
WHERE ID Between (range that you know the surname is in the correct field)

Then you can delete the old name fields and rename the new ones as you like.

DBomrrsm
 
Creating extra fields is unnecessary. You can just do:

Code:
UPDATE mytable
SET firstname = lastname,
  lastname = firstname
WHERE id BETWEEN 101 AND 201

--James
 
Thanks guys... JamesLean - I read your mind before I read your post! That's exactly how I did it...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top