Solved

CASE WHEN is null - SQL Server

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

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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 49

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

635 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