SQLTeam.com | Weblogs | Forums

Retain Leading Zeros in Case Statement


#1

SELECT CASE removes my leading zeros from my select statement.

This is my logic:
case when lc.lunchCode IS NULL THEN s.personID ELSE lc.lunchCode END as POSID

The lc.lunchCode is a text field. And if i do a straight select statement like (SELECT lc.lunchCode), then everything is fine. But i need the case statement.

I also can't pad the number. The lunch codes are not a fixed length.

Any help would be GREATLY appreciated!


#2

Cast s.PersonId to the same data type as lunchCode. For example:

case when lc.lunchCode IS NULL THEN CAST(s.personID as VARCHAR(32)) ELSE lc.lunchCode END as POSID

#3

Absolutely perfect James! I've been trying everything under the sun for the past 2 hours. You're awesome.