The case for CASE

By Bill Graziano on 06 October 2000 | 4 Comments | Tags: SELECT


We get many questions asking whether it's possible to use an IF clause inside a SELECT statement. Well, not exactly. But you can use CASE to do the exact same thing.

CASE comes in two basic formats, simple and searched. Let's start with an example of what a simple CASE statement in a query looks like:

SELECT au_fname, au_lname, 
  CASE state
    WHEN 'CA' THEN 'California'
    WHEN 'KS' THEN 'Kansas'
    WHEN 'TN' THEN 'Tennessee'
    ELSE 'Some Other State'
  END AS StateName
FROM pubs.dbo.authors

This query will return three columns. The third column, StateName, is constructed using the CASE statement. The CASE statement is inclosed by the keywords CASE and END. The CASE statement evaluates the field state. If the field contains 'CA' then the CASE statement returns 'California'. 'KS' returns 'Kansas' and 'TN' returns 'Tennessee'. If the value of State doesn't match any of the WHEN expressions, the expression in the ELSE clause is returned. The AS StateName aliases the column name to StateName.

The second form of CASE is the searched CASE. In this format, CASE is evaluating a series of boolean expressions. It returns the first expression that evaluates to true.

SELECT CASE
    WHEN state = 'KS' THEN 'In Kansas'
    WHEN state = 'ND' THEN 'Up North'
    ELSE 'Click your heels'
  END
FROM pubs.dbo.authors

This CASE statement will run through each WHEN clause until it finds a true one. If they all evaluate to false it will return the ELSE clause. In both cases if there is no ELSE clause it will return a NULL.

In the second example, each boolean expression is independant of each other statement. In this example:

SELECT CASE
    WHEN state = 'KS' THEN 'In Kansas'
    WHEN city = 'New York' THEN 'East I think'
    ELSE 'Click your heels'
  END
FROM pubs.dbo.authors

I can actually compare different columns. Remeber that it searches through the WHEN clauses from top to bottom so order them carefully. Happy coding :)

Discuss this article: 4 Comments so far. Print this Article. This page has been read 75,588 times.

If you like this article you can sign up for our newsletter. We send it out each week that we post a new article. There's an opt-out link at the bottom of each newsletter so it's easy to unsubscribe at any time.

Email Address:

Email ThisSubscribe to this feedKick itSave to del.icio.usView blog reactions

Related Articles

Joining to the Next Sequential Row (2 April 2008)

Writing Outer Joins in T-SQL (11 February 2008)

How to Use GROUP BY with Distinct Aggregates and Derived tables (31 July 2007)

How to Use GROUP BY in SQL Server (30 July 2007)

SQL Server 2005: Using OVER() with Aggregate Functions (21 May 2007)

Server Side Paging using SQL Server 2005 (4 January 2007)

Using XQuery, New Large DataTypes, and More (9 May 2006)

Counting Parents and Children with Count Distinct (10 January 2006)

Other Recent Forum Posts

Foreign Key References Invalid Table (0 Replies)

Failed to import Excel Data (1 Reply)

pending transactions (2 Replies)

hardware requairment for 1200 concurrent connetion (6 Replies)

Question about considering scaling while designing (5 Replies)

SQL Server Job fails (6 Replies)

database purging (6 Replies)

Installation Error - packagefortheweb (3 Replies)

Subscribe to SQLTeam.com

Weekly SQL Server newsletter with articles, forum posts, and blog posts via email:

SQLTeam.com Articles via RSS

SQLTeam.com Weblog via RSS

- Advertisement -

SQL Server Jobs