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)
 Why is this returning NULL

Author  Topic 

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-03-18 : 17:12:02
Greetings

When doing following select,

SELECT CASE STDEV(DefinitionValue)
WHEN NULL THEN 70641.1907289367
WHEN 0 THEN 70641.1907289367
ELSE STDEV(DefinitionValue)
END
FROM #WorkLoad
WHERE DefinitionDate >= DATEADD(MONTH, DATEDIFF(MONTH, '19000401', CURRENT_TIMESTAMP), '19000101')
AND DefinitionDate <= DATEADD(dd,DATEDIFF(dd,0, GETDATE() ),0)


whereas this works as wanted but would rather not call STDEV so many times. Am I missing something.


SELECT CASE
WHEN STDEV(DefinitionValue) IS NULL THEN 70641.1907289367
WHEN STDEV(DefinitionValue) = 0 THEN 70641.1907289367
ELSE STDEV(DefinitionValue)
END
FROM #WorkLoad
WHERE DefinitionDate >= DATEADD(MONTH, DATEDIFF(MONTH, '19000401', CURRENT_TIMESTAMP), '19000101')
AND DefinitionDate <= DATEADD(dd,DATEDIFF(dd,0, GETDATE() ),0)

Thanks

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-03-18 : 17:16:10
I think because writing
CASE STDEV(DefinitionValue) WHEN NULL 

is the same as
CASE when STDEV(DefinitionValue)=NULL 

which would never hold true.
Go to Top of Page

bmatthews
Starting Member

8 Posts

Posted - 2009-03-19 : 02:48:11
Maybe try changing

CASE STDEV(DefinitionValue) WHEN NULL

to this?
CASE STDEV(DefinitionValue) IS NULL

I think that might work.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-19 : 12:49:09
quote:
Originally posted by yosiasz

Greetings

When doing following select,

SELECT CASE STDEV(DefinitionValue)
WHEN NULL THEN 70641.1907289367
WHEN 0 THEN 70641.1907289367
ELSE STDEV(DefinitionValue)
END
FROM #WorkLoad
WHERE DefinitionDate >= DATEADD(MONTH, DATEDIFF(MONTH, '19000401', CURRENT_TIMESTAMP), '19000101')
AND DefinitionDate <= DATEADD(dd,DATEDIFF(dd,0, GETDATE() ),0)


whereas this works as wanted but would rather not call STDEV so many times. Am I missing something.


SELECT CASE
WHEN STDEV(DefinitionValue) IS NULL THEN 70641.1907289367
WHEN STDEV(DefinitionValue) = 0 THEN 70641.1907289367
ELSE STDEV(DefinitionValue)
END
FROM #WorkLoad
WHERE DefinitionDate >= DATEADD(MONTH, DATEDIFF(MONTH, '19000401', CURRENT_TIMESTAMP), '19000101')
AND DefinitionDate <= DATEADD(dd,DATEDIFF(dd,0, GETDATE() ),0)

Thanks




why use case when? you could just do this to avoid repetition


SELECT COALESCE(NULLIF(STDEV(DefinitionValue),0),70641.1907289367)

FROM #WorkLoad
WHERE DefinitionDate >= DATEADD(MONTH, DATEDIFF(MONTH, '19000401', CURRENT_TIMESTAMP), '19000101')
AND DefinitionDate <= DATEADD(dd,DATEDIFF(dd,0, GETDATE() ),0)
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-03-23 : 10:10:49
I like to use case because it is easier to read. but in this case i See the benefit of your approach. kaching! Thanks Visakh!
Go to Top of Page
   

- Advertisement -