Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for NULL in a CASE statement with a Scalar Function?

Tags:

How do you check for NULL in a CASE statement, when you're using a Scalar Function?

My original query was ... but it fails

SELECT  CASE dbo.fnCarerResponse('')           WHEN NULL THEN 'Pass'           ELSE 'Fail'         END 

I read the SO question about using IS NULL, like so ...

SELECT  CASE dbo.fnCarerResponse('') IS NULL            WHEN NULL THEN 'Pass'           ELSE 'Fail'         END         

but this gives the incorrect syntax near the keyword is error

Can you have a Scalar Function in the CASE ?

like image 424
SteveC Avatar asked May 23 '12 13:05

SteveC


1 Answers

You are using the wrong style of CASE - you need to use CASE WHEN <expression> THEN not CASE <expression> WHEN <expression> then:

SELECT CASE   WHEN dbo.fnCarerResponse('') IS NULL  THEN 'Pass'  ELSE 'Fail' END 
like image 103
Aaron Bertrand Avatar answered Oct 02 '22 00:10

Aaron Bertrand