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)
 IF statement in sql

Author  Topic 

u2envy1
Yak Posting Veteran

77 Posts

Posted - 2008-11-27 : 07:00:11
How do I add an IF in a select statement

If the following result is True I want a Y in the column & a N if false.
I tried this but it does not work.
IF (RosterShift.RSH_DayOff_B) THEN 'Y' ELSE 'N'

SELECT RosterShift.RSH_DayOff_B
FROM Roster

bjoerns
Posting Yak Master

154 Posts

Posted - 2008-11-27 : 07:12:13
You want to change values, not code. So you use the CASE expression, not the IF statement.
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-11-27 : 07:13:11
use case statement
syntax is like...

CASE input_expression 
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
]
END
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-11-27 : 07:14:55
An example:

select
name,
CASE type_desc
WHEN 'SYSTEM_TABLE' THEN 'Y'
ELSE 'N'
END as System
from sys.objects

Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

bjoerns
Posting Yak Master

154 Posts

Posted - 2008-11-27 : 07:18:37
Sakets, did you read this?
http://weblogs.sqlteam.com/jeffs/archive/2007/05/03/60195.aspx
Go to Top of Page

u2envy1
Yak Posting Veteran

77 Posts

Posted - 2008-11-27 : 07:20:47
Thx guys....
Much appreciated.
Go to Top of Page
   

- Advertisement -