dbTalk Databases Forums  

SQL UPDATE Conditionally

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL UPDATE Conditionally in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Orwell
 
Posts: n/a

Default SQL UPDATE Conditionally - 08-05-2003 , 01:43 PM






This is probably a stupid question, but here goes: I am trying to
update a table differently based on what is in one of the columns
within the table. For example, I want to set Fld1 to be "On" if Fld2
is "A", and I want to set Fld1 to be "Off" if Fld2 is "B". This is
easy to do in two SQL statements, but I was wondering if there's some
way to do it in a single SQL statement. Thus far, I have had no luck
in figuring out how to do this. Any ideas would be appreciated!!

Reply With Quote
  #2  
Old   
John Gilson
 
Posts: n/a

Default Re: SQL UPDATE Conditionally - 08-05-2003 , 02:30 PM






"Orwell" <orwellnelson (AT) usa (DOT) com> wrote

Quote:
This is probably a stupid question, but here goes: I am trying to
update a table differently based on what is in one of the columns
within the table. For example, I want to set Fld1 to be "On" if Fld2
is "A", and I want to set Fld1 to be "Off" if Fld2 is "B". This is
easy to do in two SQL statements, but I was wondering if there's some
way to do it in a single SQL statement. Thus far, I have had no luck
in figuring out how to do this. Any ideas would be appreciated!!
UPDATE T
SET Fld1 = CASE WHEN Fld2 = 'A'
THEN 'On'
ELSE 'Off'
END
WHERE Fld2 = 'A' OR Fld2 = 'B'

Regards,
jag




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.