Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how do I check if a field is null?

Author  Topic 

rsegecin
Yak Posting Veteran

82 Posts

Posted - 2007-07-02 : 15:58:37
hi guys, I'm using SQLServer 2005 and I was wondering how I check if some variable is null or not? I tryied to do as the code show but it doesn't work:

declare @number int

set @number = (select number from numbers where number_id = 1)

if (@number = null)
begin
set @number = 12
end

Thank you very much

sshelper
Posting Yak Master

216 Posts

Posted - 2007-07-02 : 15:59:50
You have to use the IS NULL condition and not the = sign:

if @Number IS NULL
SET @Number = 12

SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-02 : 16:00:33
select @number = number from numbers where number_id = 1
set @number = isnull(@number, 12)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

rsegecin
Yak Posting Veteran

82 Posts

Posted - 2007-07-02 : 16:01:13
thank you both =D
Go to Top of Page
   

- Advertisement -