Coldfusion blog - by phil porter
   

fearlessFusion.com is my Blog and Online portfolio; I am currently the Director of Visualweb Ltd. Read More


SQL - The case for CASE

The Transact SQL Case statement is a particularly useful piece of syntax to know as it can be used to solve many potentially tricky SQL Server programming tasks without resorting to more computationally intensive procedures such as using cursors, temporary tables or processing of the results set in a programming. One such use is to create a column in a results set with differing values depending on the value of a particular column. For example, the pubs SQL Server sample database contains a publishers table. In this table is a country column which specifies the name of the country in which the publisher is based. If you wanted to write a SQL query to return the details of the publishers plus a shipping column that would specify whether the publisher was based in the USA or overseas then the following query could be used:
select *,
case when country = 'USA' then 'Domestic' else 'Overseas' end Shipping
from publishers
order by Shipping
This query will return a value of Domestic in the Shipping column for publishers based in the USA, or Overseas for all the non-USA based publishers. The Case statement can also be used to identify rows containing null values for particular columns. For example, the following query will show which rows contain a null value for the country column:
select *,
case when country is null then 'Yes' else 'No' end MissingCountry
from publishers
It is also possible to specify multiple When statements:
select *,
case when country = 'USA' then 'Domestic'
when country is null then 'Unknown'
else 'Overseas' end Shipping
from publishers
order by Shipping
The Case statement can also be used to specify the selection of data from different columns depending on the value of other columns. In the SQL statement below, the Address column will contain the city and state details for USA publishers, but for overseas publishers the Address column will contain the city followed by the country:
select *,
case when country = 'USA' then city + ', ' + state
when country is null then 'Unknown Address'
else city + ', ' + country end Address
from publishers


Comments (Comment Moderation is enabled. Your comment will not appear until approved.)

Sponsored Links

 

 




Calendar

« July 2008 »
M T W T F S S
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      


Recent Comments

Multiple SQL inserts: Oracle, Mysql, Mssql

JAYESH TALSANIYA said: in mssql u cannot use parentheses in select statement. use as below: INSERT INTO [TABLE] (column1,... [More]

Multiple SQL inserts: Oracle, Mysql, Mssql

JD said: I found with MSSQL, I couldn't use parentheses in my select statements for insert. So, from your ex... [More]

BlogCFC ping list and timeouts

Will Wilson said: Nice :) that will come in handy, thanks! [More]

Is this a Coldfusion 8 BUG

Barb said: I'm having the same problem . . . when I start CF8, it seems to hang during or just after the user M... [More]

Is this a Coldfusion 8 BUG

João Fernandes said: Phil, when you install CF8 they give you an option to install LCDS within CF. The messageBrokerServl... [More]





W3C Design Gallery

Löwenherz im wilden Westen natalia ventre
CSSWerk holgerkoenemann.com
2Creative Roy Arellano - Web Designer