Solved

SQL CASE Statement

Posted on 2008-06-17
8
1,616 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:ScottPletcher
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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:ScottPletcher
ScottPletcher 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now