-1
SELECT   ProductNumber, Category =
      CASE ProductLine
         WHEN 'R' or 'r' THEN 'Road'
         WHEN 'M' or 'm' THEN 'Mountain'
         WHEN 'T' or 't' THEN 'Touring'
         WHEN 'S' or 's' THEN 'Other sale items'
         ELSE 'Not for sale'

My basic requirement is to use 'OR' with CASE (if possible) , Please suggest if i am doing anything wrong.

Taryn
  • 242,637
  • 56
  • 362
  • 405
ashish
  • 523
  • 4
  • 14
  • 27

4 Answers4

5

A different way to write case comes close, but then you have to repeat the variable:

CASE
  WHEN ProductLine in ('R', 'r') THEN 'Road'
  WHEN ProductLine in ('M', 'm') THEN 'Mountain'

Or a like character class:

CASE
  WHEN ProductLine like '[Rr]' THEN 'Road'
  WHEN ProductLine like '[Mm]' THEN 'Mountain'

Note that in SQL, string comparison is case-insensitive by default. You can alter the default by setting a case-sensitive database collation, but that's fairly unusual.

Andomar
  • 232,371
  • 49
  • 380
  • 404
0

you can also write

select
    ProductNumber,
    case upper(ProductLine)
        when 'R' then 'Road'
        when 'M' then 'Mountain'
        when 'T' then 'Touring'
        when 'S' then 'Other sale items'
        else 'Not for sale'
    end as Category

You can set your collation to case insensitive and just compare ProductLine with letters

Roman Pekar
  • 107,110
  • 28
  • 195
  • 197
  • Thanks Roman Pekar , but my requirement will be solved if i use 'OR' with case statement as i have mentioned in the sample code, My real problem is different than i mentioned in the sample code .. – ashish Oct 25 '12 at 16:40
0

Simply you could do as;

SELECT   ProductNumber, CASE LOWER(ProductLine) 
                            WHEN 'r' THEN 'Road'
                            WHEN 'm' THEN 'Mountain'
                        END AS Category
FROM TableName

For more details: Click here

Kaf
  • 33,101
  • 7
  • 58
  • 78
0

I would recommend case fall-through, but that's not allowed in SQL. However, there's a very good answer on SQL's work-around for fall-through: Can the SQL Case Statment fall through?

Community
  • 1
  • 1
LConrad
  • 816
  • 1
  • 11
  • 20