Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 448
Kajbo Avatar asked Aug 31 '26 00:08

Kajbo


1 Answers

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);
like image 95
Lukasz Szozda Avatar answered Sep 02 '26 16:09

Lukasz Szozda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!