Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL CASE Statement

Posted on 2008-06-17
8
Medium Priority
?
1,634 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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!

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

664 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