Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case and Null

Status
Not open for further replies.

Mdavis123

Programmer
Nov 12, 2001
56
US
I'm using this select statement, how do I account for nulls?

select SUM(CASE c.Gender WHEN 'Male' THEN 1 ELSE 0 END) AS Male,
SUM(CASE c.Gender WHEN 'Female' THEN 1 ELSE 0 END) AS Female,
SUM(CASE c.Gender WHEN 'Unknown' THEN 1 ELSE 0 END) AS Unknown,
SUM(CASE c.Gender WHEN null THEN 1 ELSE 0 END) AS nulls

From MyTable
Nulls column always shows 0

Tia
Mike D
 
Try ISNULL. The syntax is ISNULL(check_expression, replacement_expression). Check out Books Online.

-SQLBill
 
SQLbill,
This worked after I swapped the Then and Else values.

SUM(CASE c.Gender WHEN isnull(c.Gender,'T') THEN 0 ELSE 1 END) AS nulls

Not sure why,
Thanks SQLBill,
MikeD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top