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 Statement Else More Specific

Author  Topic 

webcomber
Starting Member

10 Posts

Posted - 2008-09-06 : 04:10:30
I am trying to modify a case statement that dumps everything into the else clause to be more specific with what it returned. For example, the case statement has many conditions like this:
S=
Case
when a='xx' then 'Group A'
when b = 'yy' then 'Group B'

This goes on for about 3 pages and then finally says
else 'Group 800'

What I would like to do is more clearly define Group 800 in the else clause and only populate it when it meets further conditions such as
when Division NOT IN ('a','b','c','d')
AND Item NOT IN ('a','b','c','d')
AND Name NOT IN ('a','b','c','d')
AND GROUP = 'xxx'

Can I just add this to the else clause? I would like the result to include all the exclusions in the prior case statements, because those properties belong only to other groups in those case statements, and then return the rest with the additional exclusions like above. I have not seen any examples of this anywhere.

Or is this not handled in a case statement at all?
Thanks -
SD

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-06 : 05:00:11
Try

S=
Case
when a='xx' then 'Group A'
when b = 'yy' then 'Group B'
.
.
.
else
case when Division NOT IN ('a','b','c','d')
AND Item NOT IN ('a','b','c','d')
AND Name NOT IN ('a','b','c','d')
AND GROUP = 'xxx'
then 'Group 800'
else 'some other group'
end
end

[/code]

Madhivanan

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

webcomber
Starting Member

10 Posts

Posted - 2008-09-06 : 21:45:12
Great - thanks!
Go to Top of Page
   

- Advertisement -