Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to use IF in SELECT

Author  Topic 

kirknew2SQL
Posting Yak Master

194 Posts

Posted - 2008-01-28 : 15:51:55
I need to to populare a column based oon the contents of another column. I thught an IF would be the right command but i can't get the syntex correct. Should I use an IF or should i use a different method?

update MyTest
set Action = IF status in ('Hospital Phone Number', 'Fax Tone', 'Wrong Phone Number- Not A Doctors Office',
'Number Not In Service', 'Unknown Home Answering Machine') then 'DELETE' elseif status in ('xxx', 'yyy') then 'OK'

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-01-28 : 16:00:28
Use CASE.

UPDATE MYTEST
SET ACTION=
CASE
WHEN STATUS IN (....) THEN 'DELETE'
WHEN STATUS IN (....) THEN 'OK'
END


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2008-01-28 : 16:00:52
Use CASE.


update MyTest
set Action = CASE WHEN [status] in ('Hospital Phone Number', 'Fax Tone', 'Wrong Phone Number- Not A Doctors Office',
'Number Not In Service', 'Unknown Home Answering Machine') then 'DELETE'
WHEN [status] in ('xxx', 'yyy') then 'OK'
END


Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-01-28 : 16:01:36
try this

update MyTest
set Action =
case when status in
('Hospital Phone Number', 'Fax Tone', 'Wrong Phone Number- Not A Doctors Office',
'Number Not In Service', 'Unknown Home Answering Machine') then 'DELETE'
when status in ('xxx', 'yyy') then 'OK' end
Go to Top of Page

kirknew2SQL
Posting Yak Master

194 Posts

Posted - 2008-01-28 : 16:06:21
Thanks for the help. I'll know the proper command now. :)
Go to Top of Page
   

- Advertisement -