More SQL help, please
July 15, 2008 12:25 PM
Subscribe
Another SQL question (*sigh*), but this time a bit more complicated.
There is a column in my database called 'Contact'. It is currently null for about 140k of the 148K entries. There are two other columns called 'Lastname' and 'Firstname' in my database, which are either populated with data or one or the other or both are null.
I want all entries in the column 'Contact' that are null to be a combination of the 'Firstname' and 'Lastname' columns by taking the value in 'Firstname' appending a space then appending the value in 'Lastname' to it, and sticking that into the 'Contact' column. Because some entries may have only a first name, only a last name or neither, I still want to stick with that logic, since I want the information in the Contact column, even if it's just copying a space back into it.
How can I go about doing this? I'm running SQL Server 2005 and have the management studio, FWIW.
BTW, I can do a select statement that gets the 140k entries in which the contact value is null, but I want to make sure that I overwrite those values only, not the other 8k or so entries whose contact values are not null.
Thanks to all in advance!
posted by omnipotentq to computers & internet (7 comments total)
UPDATE TABLE
Set Contact = Firstname + " " + Lastname
where contact is null and firstname is not null and lastname is not null
UPDATE TABLE
Set Contact = Lastname
where contact is null and firstname is null and lastname is not null
UPDATE TABLE
Set Contact = Firstname
where contact is null and firstname is not null and lastname is null
UPDATE TABLE
Set Contact = " "
where contact is null
Should do what you need. (Yeah, it can be done in one horribly complex query or four really simple queries...)
posted by arniec at 12:36 PM on July 15, 2008