Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum DBA Videos
internet.com

» Database Journal Home
» DBA Videos
» Database Articles
» Database Tutorials
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» DBA Jobs
» Sitemap

News Via RSS Feed



follow us on Twitter

Marketplace Partners
Be a Marketplace Partner

internet.commerce
Be a Commerce Partner


















Facebook Seen as Biggest Threat to IT Security

Google Teases With Chrome-Powered Tablet

Palm Leads Rally Ahead of Cisco's Results

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Database Journal | DBA Support | SQLCourse | SQLCourse2







Business Intelligence Developer (IL)
Next Step Systems
US-IL-Elk Grove Village

Justtechjobs.com Post A Job | Post A Resume

Featured Database Articles

MS SQL

March 18, 2001

Case Statement Tricks

By Neil Boyle

The CASE statement is a very flexible tool. Here are just a few of the tricks you can work with it.

Item translation

One of the simplest things you can do is to derive a columns contents based on it's contents, or the contents of another data item.

Click here for code example 1.

Titling subtotals

You can use CUBE and ROLLUP to produce totals and subtotals within a result set, but because SQL Server places NULLS for non-totaled columns in the total rows, they do not always look very pretty. In this example we use the CASE statement to place emphasis on the subtotal lines.

Click here for code example 2.

Simplifying Output

This batch lists of sales by store, and gives a general indication of the stores performance against the average quantity of sales per store.

Click here for code example 3.

This query batch starts by calculating the average of total sales per store, which is stored in the variable @storeAvg, then that average is used to rate the stores sales performance.

Grouping data into ranges

This example calculates the number of orders in certain size ranges. It also shows off the power of SQL as a set-based language: to code this as in a procedural language like VB would not be able to do this in a single statement.

Click here for code example 4.

Complex Sequencing

CASE statements can be used in an 'ORDER BY clause to order non-sequential data. This example sorts stores by name, but puts all the stores in California at the top of the list.

SELECT stor_name, state
FROM   stores
ORDER  BY CASE state
          WHEN 'CA' then 1
          ELSE 2
       END,
       stor_name

Further reading

This article discusses how to increase performance of complex UPDATE statements by using CASE.

This SQL 2000 article contains a section that shows how to use the CASE statement in a User Defined Function to enforce complex declarative constraints



Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MS SQL Archives








Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
alter a table from a bk file which is the publication, msg 4929 DLu 4 February 5th, 04:35 PM
Service broker bobbo 0 February 3rd, 12:42 PM
Job properties yogesphu 2 January 28th, 12:47 AM
SSIS Pavkage deepa 2 January 27th, 08:02 PM









The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers