user3140169
user3140169

Reputation: 221

No string output for char conversion

I have a Char(1) Nullable column (District) that I want to output "Not assigned" when NULL;

I've tried

SELECT CASE District
        WHEN NULL
            THEN 'Not Assigned'
        ELSE District
        END AS District
    ,[Name]
    ,AgencyType
FROM cli_Agency

and

SELECT CASE CONVERT(VARCHAR, District)
        WHEN NULL
            THEN 'Not Assigned'
        ELSE CONVERT(VARCHAR, District)
        END AS District
    ,[Name]
    ,AgencyType
FROM cli_Agency

But Null is displayed instead of 'Not Assigned'.

Upvotes: 1

Views: 22

Answers (1)

John Cappelletti
John Cappelletti

Reputation: 81970

Just a syntax issue

...
CASE WHEN District IS NULL THEN 'Not Assigned' ELSE District END AS District,
...

You could also write it as

IsNull(District,'Not Assigned') as District,

Or Even

coalesce(District,'Not Assigned') as District,

Upvotes: 3

Related Questions