Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL NULL Condition

Tags:

sql

I'm new to development. In sql I have written a stored procedure and it is working correctly but I just wanted to make sure it is done the correct way.

Here I am using an IF statement in sp to check whether date column in a table is NULL or not:

  if ((select sdate 
       from tbla 
         where id='3') = Null)
begin
  some query
end

Will this work for all cases or do I need to check for ''(empty) also?

like image 388
user993935 Avatar asked Feb 13 '26 15:02

user993935


2 Answers

In SQL null is not equal to anything, including other nulls. You need to use is null instead of the = operation.

like image 151
Sergey Kalinichenko Avatar answered Feb 15 '26 04:02

Sergey Kalinichenko


Empty and NULL are not the same thing, so you will need to check for an empty string separately. You also should use is null rather than = null to check if a column is null.

like image 27
Shaun Avatar answered Feb 15 '26 04:02

Shaun