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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Case When doesnt work (for me)

Author  Topic 

Antinsh
Starting Member

16 Posts

Posted - 2008-05-21 : 09:24:57
Can someone tell me why this thing in the end return Null?

declare @uz_id uniqueidentifier
set @uz_id=null
set @uz_id=
case @Uz_ID
when null then '00000000-0000-0000-0000-000000000000'
else @Uz_ID
end
select @uz_id

...it should return '00000000-0000-0000-0000-000000000000' in my opintion

P.S. When I say return i mean the value that is shown after 'select @uz_id' is executed:)

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-05-21 : 09:33:06
change u r case to

case when @Uz_ID is null then
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-21 : 09:34:05

declare @uz_id uniqueidentifier
set @uz_id=null
set @uz_id=
case when @Uz_ID is null then '00000000-0000-0000-0000-000000000000'
else @Uz_ID
end
select @uz_id

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-21 : 09:34:23
In my humble opinion, the code above should return NULL.
Why? Because you can't check for NULL that way.

Try this
declare	@uz_id uniqueidentifier

set @uz_id = null

set @uz_id = case
when @Uz_ID is null then '00000000-0000-0000-0000-000000000000'
else @Uz_ID
end

select @uz_id



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-21 : 09:34:53



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-21 : 09:35:16


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -