YoUwRonG
YoUwRonG

Reputation: 35

SQL Server : SELECT CASE return NULL

I'm trying to do a SELECT in which if the CurrencyCode is = '', then I would like the result to be return as NULL.

For example:

INSERT INTO Currency (CurrencyCode, CurrencyName)
    SELECT 
        CurrencyCode,
        CASE
            WHEN CurrencyCode = ''
              THEN NULL
        END
    FROM Contract

Can it be done?

Upvotes: 0

Views: 16367

Answers (1)

Michael Mairegger
Michael Mairegger

Reputation: 7301

There are two possibilities either you use CASE WHE... THEN... ELSE... END OR NULLIF

SELECT CurrencyCode,
NULLIF(CurrencyCode,''),
CASE
    WHEN CurrencyCode = ''
    THEN NULL
    ELSE CurrencyCode
END
FROM Contract

Upvotes: 5

Related Questions