Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a column's value is null in SQL Server

Can't believe I am stuck with this but how can I check that value I am returning is null in select statement

IF EXISTS(SELECT TU.Tagged FROM TopicUser TU 
          WHERE TU.TopicId = @TopicId and TU.UserId = @UserId)
BEGIN
    --do stuff
END

The value of TU.Tagged is NULL but yet it does go into the condition. In mind it does not exist.

like image 706
Arianule Avatar asked Oct 20 '25 04:10

Arianule


1 Answers

It looks like you need something like:

IF EXISTS(SELECT TU.Tagged 
          FROM TopicUser TU 
          WHERE TU.TopicId = @TopicId 
               AND TU.UserId = @UserId 
               AND TU.Tagged IS NOT NULL)
BEGIN
    --do stuff

END

Otherwise, you're checking only if records meeting your criteria exist, but those records could have a NULL value in the TU.Tagged column.

like image 130
Andrey Korneyev Avatar answered Oct 22 '25 18:10

Andrey Korneyev



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!