Solved

CASE WHEN is null - SQL Server

Posted on 2013-06-14
8
1,093 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 69

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

816 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

11 Experts available now in Live!

Get 1:1 Help Now