datetime century date into pad dynamic cursor money percent sp job isnumeric isdate over update
SQLUSA.com
SQL 2008 GRAND SLAM ON 49 CD
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server Training SQL 2005 Scripts SQL 2008 Articles
SQL JOBS News Format Developer
How to use the CASE expression to decode a Boolean column?

Execute the following SQL Server T-SQL sample scripts in Management Studio Query Editor to illustrate the applicaton of the CASE function to translate Boolean data (0,1) to words.

-- SQL case function as translate function

USE AdventureWorks;

GO

-- Create table for demo

CREATE TABLE #Celebrity (

  [ID]      INT    IDENTITY ( 1 , 1 )    PRIMARY KEY,

  [Name]    VARCHAR(64)    NOT NULL UNIQUE,

  Gender    BIT    NULL, -- Boolean 1:male 0:female

  BirthDate SMALLDATETIME    NOT NULL,

  ModifiedDate datetime default (getdate()))

GO

 

-- Populate Celebrity table

-- SQL insert - insert into

INSERT #Celebrity ([Name], Gender, BirthDate) VALUES ('Elvis Presley', 1, '1935-01-08')

INSERT #Celebrity ([Name], Gender, BirthDate) VALUES ('Marilyn Monroe', 0, '1926-06-01')

INSERT #Celebrity ([Name], Gender, BirthDate) VALUES ('Doris Day', 0, '1924-04-03')

INSERT #Celebrity ([Name], Gender, BirthDate) VALUES ('Britney Spears', 0, '1981-12-02')

INSERT #Celebrity ([Name], Gender, BirthDate) VALUES ('Frank Sinatra', 1, '1915-12-15')

INSERT #Celebrity ([Name], Gender, BirthDate) VALUES ('Lisa Loeb', null, '1968-03-11')

INSERT #Celebrity ([Name], Gender, BirthDate) VALUES ('Don Henley', 1, '1947-07-22')

INSERT #Celebrity ([Name], Gender, BirthDate) VALUES ('Madonna', 0, '1958-09-16')

INSERT #Celebrity ([Name], Gender, BirthDate) VALUES ('Tom Jones', 1, '1940-06-07')

GO

/* Error message upon duplicate [Name] insert attempt:

Msg 2627, Level 14, State 1, Line 6

Violation of UNIQUE KEY constraint 'UQ__#Celebri__737584F600200768'. Cannot insert

duplicate key in object 'dbo.#Celebrity'.

*/

 

-- SQL CASE function to decode a Boolean column

SELECT   Celebrity = [Name],

         Gender = CASE Gender

                    WHEN 0 THEN 'Female'

                    WHEN 1 THEN 'Male'

                    ELSE ''

                  END,

         BirthDate = CONVERT(VARCHAR,BirthDate,107),

         [Age/WouldBeAge] = DATEDIFF(YEAR,BirthDate,getdate())

FROM     #Celebrity

ORDER BY [Age/WouldBeAge] DESC

GO

 

DROP TABLE #Celebrity

GO

-- Results

Celebrity Gender BirthDate Age/WouldBeAge
Frank Sinatra Male 12/15/1915 94
Doris Day Female 4/3/1924 85
Marilyn Monroe Female 6/1/1926 83
Elvis Presley Male 1/8/1935 74
Tom Jones Male 6/7/1940 69
Don Henley Male 7/22/1947 62
Madonna Female 9/16/1958 51
Lisa Loeb 3/11/1968 41
Britney Spears Female 12/2/1981 28

 

Related articles:

http://www.sqlusa.com/bestpractices/training/scripts/casefunction/

In T-SQL, use CASE...WHEN in place of IF...THEN

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
Microsoft SQL Server 2012 Training Videos at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Accounting
Administrative
Advertising
Arts
Architecture
Banking
Business Intelligence
Career Jobs
Celebrity
Computer
Consulting
Customer Service
Education
Engineering
Entertainment
Entry Level
Executive
Federal
Finance
Government
Hardware
Healthcare
Hospital
Human Resources
Information Technology
Insurance
Internet
Job Openings
Laboratory
Law Enforcement
Legal
Logistics
Manufacturing
Marketing
Medical
Military
Nursing
Pharmaceutical
Physician
Public Relations
Publishing
Real Estate
Restaurant
Retail
Sales
Social Media
Software
SQL Database
Telecomm
Therapist
Training
Transportation
Truck Driver
Travel
Web
Work from Home

FREE SS SQL / BI OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011 Microsoft Community Contributor 2012

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts

JOIN US ON TWITTER

Copyright 2005-2012, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.