Groups | Blog | Home
all groups > sql server (microsoft) > june 2006 >

sql server (microsoft) : IF within CASE


gg NO[at]SPAM xemaps.com
6/20/2006 6:34:13 AM
Why does this work:

declare @x char(1)
set @x = 'a'

print case @x
when 'a' then
case when 50 > 40 then
'50>40'
end
end

But this gives a syntax error on the if statement?

declare @x char(1)
set @x = 'a'

print case @x
when 'a' then
if 50 > 40
'50>40'
end
end

Can't you put an IF statement within a CASE statement?
Jens
6/20/2006 3:09:37 PM
That by design, look in the BOL, CASE id expecting a result expression
whereas IF is expecting { sql_statement | statement_block }.

HTH, Jens Suessmeyer.

---
http://www.sqlserver2005.de
---
Peter
7/4/2006 1:35:38 AM
'case' returns a result and is by that a function. 'if' on the other hand,
is a procedural language construct.

[quoted text, click to view]

dbahooker NO[at]SPAM hotmail.com
7/18/2006 2:09:07 PM
you should just try to use isnull and nullif and / or coalesce

-Aaron



[quoted text, click to view]
AddThis Social Bookmark Button