What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 17906 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > SQL Server > CASE expression in SQL SERVER 2008 ...
CGN007

CASE expression in SQL SERVER 2008

 Code Snippet posted by: CGN007 | Posted on: 7/3/2012 | Category: SQL Server Codes | Views: 606 | Status: [Member] | Points: 40 | Alert Moderator   


CASE
Evaluates a list of conditions and returns one result.

There are two types of CASE expressions in SQL

1.Simple CASE
The simple CASE expression compares an expression to a set of simple expressions to determine the result.
2.Searched CASE
The searched CASE expression evaluates a set of Boolean expressions to determine the result.

Both formats support an optional ELSE argument.

--Simple CASE expression:

CASE input_expression


WHEN when_expression THEN result_expression [ ...n ]

[ ELSE else_result_expression ]

END




--Searched CASE expression:

CASE


WHEN Boolean_expression THEN result_expression [ ...n ]

[ ELSE else_result_expression ]

END




Examples:



--Simple CASE expression:

SELECT empID,empname,CASE gender


WHEN 'M' THEN 'Male'

WHEN 'F' THEN 'Female'

ELSE 'NA'

END

FROM employee




--Searched CASE expression:

SELECT empID,empname,

CASE
WHEN gender= 'M' THEN 'Male'

WHEN gender= 'F' THEN 'Female'

ELSE 'NA'
END

FROM employee

Found interesting? Add this to:


 Responses

Jasminej
Posted by: Jasminej | Posted on: 6/29/2012 | Level: Starter | Status: [Member] | Points: 10 | Alert Moderator 

Amazing....!

Your sample statement will work without CASE ?

Have you tested it before post ?

CGN007
Posted by: CGN007 | Posted on: 7/3/2012 | Level: Silver | Status: [Member] | Points: 10 | Alert Moderator 

Thanks...
Its now Updated...

>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 5:13:33 AM