Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL CASE Statement

Posted on 2008-06-17
8
Medium Priority
?
1,638 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
  • 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 70

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 400 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 800 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 800 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

885 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