Visual studio feeds

All Visual Studio blogs in one place

Subscribe

Enter your email address:

Delivered by FeedBurner

Increase your website traffic with Attracta.com

Google+

Contact

Email:
Subject:
Message:
Anti-spam: How many eyes has a typical person?

Follow us on FB

Search

Simple VS Searched CASE expression

AddThis Social Bookmark Button
As we know, we can write case expression in SQL in two ways.  As per BOL, defination of these two diffrent formats of CASE expression:
• The simple CASE expression compares an expression to a set of simple expressions to determine the result.
• The searched CASE expression evaluates a set of Boolean expressions to determine the result. Before today, I was thinking the both are same and one can be used in place of other directly.  But yesterday while answering one of the forum questions, I have noticed that handing of NULL can be the different between two CASE formats.
Let’s
take an example DECLARE @T TABLE ( id INT NOT NULL , color INT) INSERT INTO @T (id,color) VALUES (1,1), (2,2), (3,3), (4,NULL)  Now in case statment --simpale case SELECT t.id, CASE t.color WHEN 1 THEN 'Red' WHEN 2 THEN 'Blue' WHEN 3 THEN 'Green' ELSE 'NoColor' END AS Color FROM @t t --searched case SELECT t.id, CASE WHEN t.color = 1 THEN 'Red' WHEN t.color = 2 THEN 'Blue' WHEN t.color =...(Read whole news on source site)

Home : Blog List : Chintak's Blog : Simple VS Searched CASE expression