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
 Help with the ISNULL function as it is not working

Author  Topic 

vamsidhar.tangutoori
Starting Member

19 Posts

Posted - 2012-11-14 : 16:26:05
Hi,
I am trying to use the isnull value to pass a variable, but it is not working as to replace the null value with a 0
The code is shown below and the result, please help me as to why this is not working.

CODE:
Declare @vSession_Id int

set @vSession_Id = (Select ISNULL(System_User_Id,0)
From dbo.SYSTEM_USER_SESSION
Where System_User_Id = 11)
select @vSession_Id

RESULT:
(No column name)
NULL

chadmat
The Chadinator

1974 Posts

Posted - 2012-11-14 : 16:35:51
What version? I run the following on 2008 R2, and I get 0:

CREATE TABLE #t1 (c1 int identity(1,1) , c2 int null)

insert into #t1 values (null)

Declare @vSession_Id int

set @vSession_Id = (Select ISNULL(c2,0)
From #t1
Where c1 = 1)
select @vSession_Id



Drop table #t1



-Chad
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-11-14 : 16:37:27
Maybe this query doesn't return anything

Select * From dbo.SYSTEM_USER_SESSION
Where System_User_Id = 11)
select @vSession_Id
Go to Top of Page

vamsidhar.tangutoori
Starting Member

19 Posts

Posted - 2012-11-14 : 16:39:01
I am running on SQL server 2012

quote:
Originally posted by chadmat

What version? I run the following on 2008 R2, and I get 0:

CREATE TABLE #t1 (c1 int identity(1,1) , c2 int null)

insert into #t1 values (null)

Declare @vSession_Id int

set @vSession_Id = (Select ISNULL(c2,0)
From #t1
Where c1 = 1)
select @vSession_Id



Drop table #t1



-Chad


Go to Top of Page

vamsidhar.tangutoori
Starting Member

19 Posts

Posted - 2012-11-14 : 16:43:05
Hi,
Thanks a lot i used your suggestion and changed the query to
Declare @vSession_Id int

set @vSession_Id = (Select System_User_Id
From dbo.SYSTEM_USER_SESSION
Where System_User_Id = 11)
select ISNULL(@vSession_Id, 0)

It solved the problem

quote:
Originally posted by sodeep

Maybe this query doesn't return anything

Select * From dbo.SYSTEM_USER_SESSION
Where System_User_Id = 11)
select @vSession_Id


Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2012-11-14 : 16:43:43
quote:
Originally posted by sodeep

Maybe this query doesn't return anything

Select * From dbo.SYSTEM_USER_SESSION
Where System_User_Id = 11)
select @vSession_Id




That is it I bet.

-Chad
Go to Top of Page
   

- Advertisement -