Solved

SQL CASE Statement

Posted on 2008-06-17
8
1,624 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 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

820 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