Kajbo
Kajbo

Reputation: 1158

T-SQL: check if variable is null

I'm wondering if there is a way to check if variable @varChar is NULL and variable @bit is 0.

My attempt:

if ISNULL(@varChar, false) and (@bit = 0) 
begin
    RAISERROR ('varchar was not specified', 16,1);
end

My problem is that ISNULL(check_if_null, replacement_value) does not work like this, and I can not find an alternative.

Upvotes: 6

Views: 22004

Answers (2)

Lukasz Szozda
Lukasz Szozda

Reputation: 175556

You could use IS NULL:

IF @varCHAR IS NULL AND @bit = 0
BEGIN
    RAISERROR ('varchar was not specified', 16,1);
END

Another approach is:

DECLARE @varCHAR VARCHAR(MAX) = NULL;

IF ISNULL(@varCHAR,'false') = 'false' AND @bit = 0
    RAISERROR ('varchar was not specified', 16,1);

Upvotes: 14

EzLo
EzLo

Reputation: 14189

Use IS NULL when checking if a value is null.

IF @varChar IS NULL and @bit = 0 
    RAISERROR ('varchar was not specified', 16,1);

Upvotes: 1

Related Questions