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 2000 Forums
 SQL Server Development (2000)
 using if in update statement

Author  Topic 

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2008-09-27 : 07:05:32
i've below 3 update statement

update student set status='JK' where enrolldate between 1/01/2006 and 3/31/2006;
update student set status='HI' where enrolldate between 4/1/2006 and 7/31/2006;
update student set status='UI' where enrolldate between 8/1/2006 and 12/31/2006;

my question is, how T-SQL will looks like, if me using if statement in enrolldate above update statement.

as a result, i'll have 1 update statement.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-27 : 07:15:45
what you want is case rather than if

update student set status= 
case when enrolldate between 1/01/2006 and 3/31/2006 then'JK'
when enrolldate between 4/1/2006 and 7/31/2006 then 'HI'
when enrolldate between 8/1/2006 and 12/31/2006 then 'UI'
end
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2008-09-27 : 20:28:15
if i want to put or statement, it's will looks like case when enrolldate between 1/01/2006 and 3/31/2006 or enrolldate between 1/01/2007 and 3/31/2007 then'JK'

it's possible?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-28 : 02:58:44
quote:
Originally posted by Delinda

if i want to put or statement, it's will looks like case when enrolldate between 1/01/2006 and 3/31/2006 or enrolldate between 1/01/2007 and 3/31/2007 then'JK'

it's possible?


yup its possible. have you tried?
Go to Top of Page
   

- Advertisement -