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 2000 Forums
 SQL Server Development (2000)
 CASE ELSE Problem

Author  Topic 

db_sysadmin
Starting Member

27 Posts

Posted - 2005-12-22 : 10:35:05
Hey there guys,

I have a CASE statement in a query, and I only want to count WHEN true conditions, don't want to count CASE ELSE statements...
if I have
Field = CASE
WHEN Count....
WHEN Count...
END

ELSE Others END /* IF I USE THIS STATMENT I get an extra row with the records that don't match any of the WHEN Statements.*/
I.E: OTHERS 40

If I just END it right there...
I get
I.E: NULL 40.

I don't want either of them OTHERS NOR NULL there.
What's wrong?

Thanks in Advance...

infobox
Starting Member

3 Posts

Posted - 2005-12-22 : 10:48:39
Nothing is wrong. If the ELSE argument is omitted and no comparison operation evaluates to TRUE, CASE returns NULL.

One thing that can be done to get what you want is....

Put a WHERE condition in your query for not allowing records having values other than those you have given in CASE WHEN statements.

-Chintan.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-23 : 01:00:18
Post some sample data and the result you want

Madhivanan

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

mr_mist
Grunnio

1870 Posts

Posted - 2005-12-23 : 06:06:28
Indeed. Infobox is right though, you can't just ignore the data that doesn't match to a case, you either match it, or it returns NULL. If you want to remove it from the results, then you need to filter out the data with a WHERE condition.

-------
Moo. :)
Go to Top of Page
   

- Advertisement -