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
 General SQL Server Forums
 New to SQL Server Programming
 syntax help with IF

Author  Topic 

lcsgeek
Starting Member

38 Posts

Posted - 2013-02-08 : 12:39:47
Can someone help me with this syntax? Thanks much


SELECT
column1
, column2
, IF @Subject = 'Mathematics' OR @Subject = 'Reading'
BEGIN
'Applicable Value'
END
ELSE
BEGIN
'N/A'
END
AS brSpringTarget
FROM ...

I want to check the value of the @Subject paramter and act accordingly.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-08 : 12:45:42
Use a case expression instead of the IF clause. IF is a control flow construct, which cannot be used in this context
SELECT
column1
, column2
, CASE
WHEN @Subject = 'Mathematics' OR @Subject = 'Reading' THEN 'Applicable Value'
ELSE 'N/A'
END brSpringTarget
FROM ...
Go to Top of Page

lcsgeek
Starting Member

38 Posts

Posted - 2013-02-08 : 12:59:25
Thank you James
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-08 : 12:59:47
You are very welcome - glad to help.
Go to Top of Page
   

- Advertisement -