943,779 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 46677
  • MS SQL RSS
Jun 4th, 2008
0

CASE function

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marcel1 is offline Offline
2 posts
since Jun 2008
Jun 4th, 2008
0

Re: CASE function

hi marcel1,
below select with case clause is standard SQL1999. Maybe it works also on MS SQL Server.
sql Syntax (Toggle Plain Text)
  1. SELECT x,
  2. (
  3. CASE
  4. WHEN x = 'AB' AND y LIKE 'CD%' THEN 'CD'
  5. WHEN 'ZZ' AND y LIKE 'CC%' THEN 'BA'
  6. WHEN x IN ('bbb','sss','ddg') THEN 'BB'
  7. ELSE 'unknown thing'
  8. END
  9. ) AS xx
  10. FROM yourtable WHERE ... ;
MS SQL Syntax (Toggle Plain Text)
  1. This will produce a list WITH two COLUMNS, FOR example
  2. x xx
  3. ------------
  4. AB CD
  5. sss BB
  6. 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
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008
Jun 5th, 2008
0

Re: CASE function

That works just fine.
Thanks
Marcel
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marcel1 is offline Offline
2 posts
since Jun 2008
May 20th, 2010
0
Re: CASE function
I want to thank tesu as well. This helped me out a lot!
Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
teezecrost is offline Offline
1 posts
since May 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: How to Display chinese characters in existing Database
Next Thread in MS SQL Forum Timeline: sql question from sql-ex.ru site





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC