Solved

CASE WHEN is null - SQL Server

Posted on 2013-06-14
8
1,107 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

749 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