Solved

CASE WHEN is null - SQL Server

Posted on 2013-06-14
8
1,068 Views
Last Modified: 2013-06-17
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
Comment
Question by:patriotpacer
8 Comments
 
LVL 5

Assisted Solution

by:DOSLover
DOSLover earned 125 total points
ID: 39248321
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
 

Author Comment

by:patriotpacer
ID: 39248349
This gives same results

select case when col IS null then 'No' when col IS not null then 'Yes' end from table
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 125 total points
ID: 39248374
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
 
LVL 5

Expert Comment

by:DOSLover
ID: 39248737
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:patriotpacer
ID: 39248901
My apologies.  Won't be able to test until Monday.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 125 total points
ID: 39249426
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
 
LVL 2

Assisted Solution

by:vivekkumarSharma
vivekkumarSharma earned 125 total points
ID: 39252381
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
 

Author Closing Comment

by:patriotpacer
ID: 39254259
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now