Read + Write + Report
Home | Start a blog | About Orble | FAQ | Blogs | Writers | Paid | My Orble | Login

Case statement

May 27th 2008 05:28
CASE expressions can be used in SQL anywhere an expression can be used. Example of where CASE expressions can be
used include in the SELECT list, WHERE clauses, HAVING clauses, IN lists, DELETE and UPDATE statements, and inside
of built-in functions.

Two basic formulations for CASE expression

1) Simple CASE expressions

A simple CASE expression checks one expression against multiple values. Within a SELECT statement, a simple CASE
expression allows only an equality check; no other comparisons are made. A simple CASE expression operates by
comparing the first expression to the expression in each WHEN clause for equivalency. If these expressions are
equivalent, the expression in the THEN clause will be returned.

Syntax:

CASE expression
WHEN expression1 THEN expression1
[[WHEN expression2 THEN expression2] [...]]
[ELSE expressionN]
END
Example:

DECLARE @TestVal INT
SET @TestVal = 3
SELECT
CASE @TestVal
WHEN 1 THEN ‘First’
WHEN 2 THEN ‘Second’
WHEN 3 THEN ‘Third’
ELSE ‘Other’
END

2) Searched CASE expressions

A searched CASE expression allows comparison operators, and the use of AND and/or OR between each Boolean
expression. The simple CASE expression checks only for equivalent values and can not contain Boolean expressions.

The basic syntax for a searched CASE expressions is shown below:

Syntax:

CASE
WHEN Boolean_expression1 THEN expression1
[[WHEN Boolean_expression2 THEN expression2] [...]]
[ELSE expressionN]
END
Example:

DECLARE @TestVal INT
SET @TestVal = 5
SELECT
CASE
WHEN @TestVal <=3 THEN ‘Top 3&#8242;
ELSE ‘Other’
END

63
Vote
Add To: del.icio.us Digg Furl Spurl.net StumbleUpon Yahoo


   
subscribe to this blog 


   

   

   

Comments
1 Comments. [ Add A Comment ]

Comment by Tyronne

May 27th 2008 23:04
I had no idea you could do CASE statements in SQL. That would be quite handy..

Add A Comment

To create a fully formatted comment please click here.


CLICK HERE TO LOGIN | CLICK HERE TO REGISTER

Name or Orble Tag
Home Page (optional)
Comments
Bold Italic Underline Strikethrough Separator Left Center Right Separator Quote Insert Link Insert Email
Notify me of replies
Your Email Address
(optional)
(required for reply notification)
Submit
More Posts
1 Posts
1 Posts
1 Posts
106 Posts dating from May 2008
Email Subscription
Receive e-mail notifications of new posts on this blog:
0

Jiansen Lu's Blogs

1570 Vote(s)
0 Comment(s)
37 Post(s)
1076 Vote(s)
0 Comment(s)
20 Post(s)
5541 Vote(s)
1 Comment(s)
161 Post(s)
5668 Vote(s)
49 Comment(s)
93 Post(s)
Moderated by Jiansen Lu
Copyright © 2012 On Topic Media PTY LTD. All Rights Reserved. Design by Vimu.com.
On Topic Media ZPages: Sydney |  Melbourne |  Brisbane |  London |  Birmingham |  Leeds     [ Advertise ] [ Contact Us ] [ Privacy Policy ]