theChampion
theChampion

Reputation: 4467

Set SQL Parameter Value IF IS NULL

I have parameter and i want to check if length is 0. If it is 0 then Null, else I want the same value ? What idea do you have ?

SET @gre = NULLIF(LEN(@gre), 0)

Upvotes: 1

Views: 75

Answers (3)

Michal Dymel
Michal Dymel

Reputation: 4360

Please try:

SET @gre = 
    case 
        when LEN(@gre) = 0 then NULL
        else @gre
    end

Upvotes: 0

RBarryYoung
RBarryYoung

Reputation: 56735

This should work if it's a VARCHAR:

SET @gre = NULLIF(@gre, '')

Upvotes: 3

Tobsey
Tobsey

Reputation: 3400

Use a CASE statement:

SET @gre = CASE WHEN LEN(@gre) = 0 OR @gre IS NULL THEN NULL ELSE @gre END

Upvotes: 0

Related Questions