Solved

SQL CASE Statement

Posted on 2008-06-17
8
1,627 Views
Last Modified: 2008-06-17
Hello Experts,

Can anyone who has a lot of expertise using CASE statements explain the difference between these 2 CASE Statements:

CASE WHEN WP.ShipDate IS NULL THEN 'N/A'
 ELSE 'Shipped on ' + CONVERT(VARCHAR,WP.ShipDate,101)
END as Shipped

RESULTS:
NULL
NULL
Shipped on 06/06/2008
NULL
Shipped on 06/16/2008

VS........

CASE WP.ShipDate
WHEN NULL THEN 'N/A'
 ELSE 'Shipped on ' + CONVERT(VARCHAR,WP.ShipDate,101)
END as Shipped

RESULTS:
N/A
N/A
Shipped on 06/06/2008
N/A
Shipped on 06/16/2008

How come one CASE statement ignores the N/A part, and the other doesn't.
0
Comment
Question by:pzozulka
[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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21805759
Are you sure the 2nd case statement works as you say it does?  I'm not able to get that syntax to work, but the 1st works as it should fine.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 21805813
The first one should work, not sure why it is not.

The NULL will not match on the second one, so it should always fall to the "ELSE" code.
0
 
LVL 10

Expert Comment

by:adriankohws
ID: 21806206
Actually you have just two conditions and don't require to use case, a "If" "Else" statement will work fine. I will also advise to use the checking in your codes behind instead of using SQL.
0
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
LVL 8

Author Comment

by:pzozulka
ID: 21806848
Im so sorry the Results are flip/flopped...

Here are the correct results in respect to each Case Statement...

CASE WHEN WP.ShipDate IS NULL THEN 'N/A'
 ELSE 'Shipped on ' + CONVERT(VARCHAR,WP.ShipDate,101)
END as Shipped

RESULTS: -- Correct, just tested. It is showing N/A whenever NULL.
N/A
N/A
06/06/2008

VS...

CASE WP.ShipDate
WHEN NULL THEN 'N/A'
 ELSE 'Shipped on ' + CONVERT(VARCHAR,WP.ShipDate,101)
END as Shipped

RESULTS:--The N/A part is not being presented...I just tested, it is not showing N/A whenever its NULL
NULL
NULL
06/06/2008
0
 
LVL 8

Author Comment

by:pzozulka
ID: 21806868
This is more of a theoretical question. I want to know why one type of CASE statement works, and the other does not. It IS NOT a matter between whether to use CASE vs. IF.
0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 100 total points
ID: 21806952
It is a TSQL thing...IS NULL is valid using the CASE but you have to use a field with it...you can use NULL the way it is used in your 2nd example.  Its just how TSQL works.
0
 
LVL 19

Accepted Solution

by:
folderol earned 200 total points
ID: 21807352
Null is undefined, which is not the same as a zero length string.  Technically, Null = Null makes no sense either, you have to rewrite it as TRUE = TRUE.
In the 2nd syntax, the case simply stops on an undefined value for ShipDate, so the rest of the statement is never reached.  In the 1st syntax  ShipDate is null is a simple boolean evaluation, true or false, with no possible undefined outcome, so branching to the then part always occurs.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 200 total points
ID: 21807855
The standard way to check for NULL in SQL is to use IS NULL.  This works accurately all the time.  So the first version:

CASE WHEN WP.ShipDate IS NULL THEN ...

works perfectly, as intended.

The other format of CASE:

CASE WP.ShipDate
    WHEN NULL THEN ...
    WHEN value2 THEN ...
    ...
does an equality comparison of the value in the column to the value in the WHEN.

By default, the result of NULL = NULL is also NULL, which is logical if you think of NULL as "unknown"/missing -- does an unknown/missing value equal another unknown/missing value?  That is clearly also unknown, since the values might be the same or they might not.
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

623 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