Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1180
  • Last Modified:

CASE WHEN is null - SQL Server

select 
    case col when null then 'No' else 'Yes' end 
 from table

Open in new window


The yes comes through but the No part comes back as null.

How to fix?  Using SQL Server  I've tried numerous variations with no success.
0
patriotpacer
Asked:
patriotpacer
4 Solutions
 
DOSLoverCommented:
Please try this:
select 
case when col is NULL then 'No' 
        when col is Not NULL then 'Yes' 
end as isANull
from table

Open in new window

0
 
patriotpacerAuthor Commented:
This gives same results

select case when col IS null then 'No' when col IS not null then 'Yes' end from table
0
 
QlemoC++ DeveloperCommented:
Though the expression used is redundant, it should work. The "correct" and necessary code is
select case when col is NULL then 'No'  else 'Yes' end as isNotNull from table

Open in new window

You can also try the reversed logic:
select case when col is not NULL then 'Yes'  else 'No' end as isNotNull from table

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DOSLoverCommented:
Does the column show 'NULL' when listed? or the filed contains ''? Can you please list some rows from the table, listing few of both?
0
 
patriotpacerAuthor Commented:
My apologies.  Won't be able to test until Monday.
0
 
PortletPaulCommented:
the core difference is the small word "IS" (when columnx IS NULL ...)
and if columnx might also be an empty string, then the case expression could be:

select case when ( columnx IS NULL or columnx = '' ) then 'No'  else 'Yes' end as isNotNull from table

Open in new window

0
 
vivekkumarSharmaCommented:
select 
    case isnull(col,'') when '' then 'No' else 'Yes' end 
 from table

Open in new window



Alternatively

select 
    case when col is null then 'No' else 'Yes' end 
 from table

Open in new window

0
 
patriotpacerAuthor Commented:
I appreciate all the help.  I did try every solution but all gave me the same result - which the Yes values come through but No values always came null.

I did a workaround with an "isnull" wrapped around me case statement to get it to work.

Given the pattern and knowledge posted here, I'm assuming this is still an issue on my side.  

But I do believe the solutions posted will work in most cases, so I'm going to close out this question.

I really appreciate all the help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now