|
ASP Kitchen: SQL Server Articles: How to SQL Server's Transact SQL Case function How to SQL Server's Transact SQL Case functionThe 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 language such as Visual Basic. Using the Case Statement in Select QueriesOne 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 *, 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 *, It is also possible to specify multiple When statements: select *, 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 *, Useful Development Tools
Author detailsBrett Burridge has worked as a web developer since 1997 and has developed web applications for a range of corporations, start up busiensses and educational establishments. Brett is presently employed as an Internet developer and technical writer through his own company, Winnersh Triangle Web Solutions Limited. The company produces a number of innovative products, including a range of software documentation tools, which include the ASP Documentation Tool, the .NET Documentation Tool for VB.NET and C#, and the SQL Server Documentation Tool. Other products include The Website Utility, which functions as a website error checker, search engine optimizer and ASP/ASP.NET search engine builder application. As well as the ASPAlliance, Brett has written articles for Ariadne.ac.uk, ASPToday, the software documentation portal www.softwaredocumentation.info, and has contributed recipes to the ASP.NET Developer's Cookbook. links Outside web development, Brett is interested in travelling (here are my travel logs from New York, Hong Kong and Tokyo), digital photography (here's my photo gallery), tropical fishkeeping and collecting contemporary works of art by artists such as Doug Hyde. Contact Brett by emailing ASP Kitchen: SQL Server Articles: How to SQL Server's Transact SQL Case function |
|
||||||||||||||||||||||||
| © page content copyright Brett Burridge 1998 - 2008. | |||||||||||||||||||||||||