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 2008 Forums
 Transact-SQL (2008)
 Case issue with Null value

Author  Topic 

Pete_N
Posting Yak Master

181 Posts

Posted - 2012-01-06 : 11:27:34
I am trying to do a case statement as follows

Select Case DestSortCode
WHEN NULL then 0 else 1 end as destsortcode
FROM [dbo].[Mytable]

DestSortCode is a NULL but select query is returning 1

Select DestSortCode
FROM [dbo].[MyTable]

Select query returns NULL

latch
Yak Posting Veteran

62 Posts

Posted - 2012-01-06 : 11:44:22
I think instead of using Case statement you can use ISNULL function :
"select isnull(DestSortCode,0) from [dbo].[Mytable]"

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-06 : 11:45:05
What does

SELECT DestSortCode, COUNT(*) AS Dest_Count FROM [dbo].[MyTable] GROUP BY DestSortCode

Give you?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Pete_N
Posting Yak Master

181 Posts

Posted - 2012-01-06 : 11:52:46
quote:
Originally posted by latch

I think instead of using Case statement you can use ISNULL function :
"select isnull(DestSortCode,0) from [dbo].[Mytable]"





Hi I am using this query in an execute SQL tak in a SSIS and need to return 0 if the DestSortCode in a NULL and a 1 if it isnt
Go to Top of Page

Pete_N
Posting Yak Master

181 Posts

Posted - 2012-01-06 : 12:00:56
quote:
Originally posted by Pete_N

quote:
Originally posted by latch

I think instead of using Case statement you can use ISNULL function :
"select isnull(DestSortCode,0) from [dbo].[Mytable]"





Hi I am using this query in an execute SQL tak in a SSIS and need to return 0 if the DestSortCode in a NULL and a 1 if it isnt



Sorted

Select CASE ISNULL(DestSortCode, 0)
WHEN 0 then 0 else 1 end
FROM [dbo].[MyTable]
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-06 : 12:58:56
See..this is a TABLE

Not a single variable value

HOW many rows are in the table?

You are thinking about this wrong

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2012-01-06 : 16:53:58
I think that your CASE is not returning the hoped for results because of the syntax used. In essence, you are asking if DestSortCode = NULL when you are trying to ask if DestSortCode IS NULL. If you use a slightly different syntax, you'll be asking the right question:[CODE]Select Case WHEN DestSortCode IS NULL then 0 else 1 end as destsortcode
FROM [dbo].[Mytable][/CODE]HTH

=================================================
Men shout to avoid listening to one another. -Miguel de Unamuno
Go to Top of Page
   

- Advertisement -