954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

CASE function

Please, try to help with this. I use case statement but for 2 arguments.
Down here is the idea of what i want to get:

select x case x
when x = 'AB' and y like 'CD%' then x= 'CD'
when x = 'ZZ' and y like 'CC%' then x= 'BA'
when x in ('bbb','sss','ddg') then x= 'BB'

I ll be grateful if anyone would help write right querry.
Thank in advance
Marcel

marcel1
Newbie Poster
2 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

hi marcel1,
below select with case clause is standard SQL1999. Maybe it works also on MS SQL Server.

SELECT x,  
(
  CASE
     WHEN x = 'AB' and y like 'CD%' then 'CD'
     WHEN 'ZZ' and y like 'CC%' then 'BA'
     WHEN x in ('bbb','sss','ddg') then 'BB' 
     ELSE 'unknown thing'
  END 
) AS xx
FROM yourtable where ... ;
This will produce a list with two columns, for example
x         xx
------------
AB       CD
sss       BB
wow     unknown thing

You can also omit the first attribute x from the select, if you don't need original x values.
Like x, y must also be attribute of yourTable.

Maybe this will help you.

krs,
tesu

tesuji
Master Poster
721 posts since Apr 2008
Reputation Points: 158
Solved Threads: 98
 

That works just fine.
Thanks
Marcel

marcel1
Newbie Poster
2 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

I want to thank tesu as well. This helped me out a lot!
Thanks

teezecrost
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You