replace "0" with "null" (blank) in SQL query

Homerboy

Lifer
Mar 1, 2000
30,890
5,001
126
part of my SELECT statement looks like the following:

Code:
COUNT(CASE WHEN (DATEDIFF(DAY,GETDATE(),DATE) = -1) THEN FILENO END) AS '-1',

What I'd like to be able to do is if that COUNT returns "0" that it is replaced with "NULL" so that it shows up in my report as a blank cell/value.

Any suggestions or thoughts would be appreciated.

Thanks.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
Code:
SELECT CASE WHEN COUNT(CASE WHEN (DATEDIFF(DAY,GETDATE(),DATE) = -1) THEN FILENO END) = 0 
THEN 
null 
ELSE 
COUNT(CASE WHEN (DATEDIFF(DAY,GETDATE(),DATE) = -1) THEN FILENO END) AS '-1',
 

Homerboy

Lifer
Mar 1, 2000
30,890
5,001
126
I get what is going on there, but it's throwing an error:


Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'AS'.
 

KLin

Lifer
Feb 29, 2000
30,951
1,080
126
Missing an End keyword.

You could also use

PHP:
SELECT NullIf(COUNT(CASE WHEN (DATEDIFF(DAY,GETDATE(),DATE) = -1) THEN FILENO END), 0) As '-1'

And only need one line of code.
 

Homerboy

Lifer
Mar 1, 2000
30,890
5,001
126
Missing an End keyword.

You could also use

PHP:
SELECT NullIf(COUNT(CASE WHEN (DATEDIFF(DAY,GETDATE(),DATE) = -1) THEN FILENO END), 0) As '-1'

And only need one line of code.

Yeah I figured out the issue in the initial code, but this new, single line is WAY cleaner and easy to implement (as I have dozens of these COUNT statements in the full query.

This should work awesomely.

Thanks!