views:

296

answers:

6

Is there a way we can specify values in for a case statement? The below statement doesn't execute because it thinks 53,57,82,83 etc are columns.. Is there a work around.. i googled up but found nothing that say you can't use IN case-when expression..

select 
x =
case 
    when xvalue in ([52],[57],[82],[83]) 
        then "xvalue"
    when yvalue in ([01],[02],[11]) 
        then "yvalue"
    else
        'NULL'
    end
from xyztable
+8  A: 

Don't put brackets around the numbers.

DyingCactus
Voting the oldest as answer :)
Broken Link
+2  A: 

Try this:

select 
case 
    when xvalue in (52,57,82,83) 
        then "xvalue"
    when yvalue in (01,02,11) 
        then "yvalue"
    else
        'NULL'
    end as 'x'

If you want to use the x, which I am assuming is going to be a variable you will want to define it like this:

DECLARE @x int

select 
@x = case 
    when xvalue in (52,57,82,83) 
        then "xvalue"
    when yvalue in (01,02,11) 
        then "yvalue"
    else
        'NULL'
    end
RandomBen
@Random: `X= ` is the same as ` as x`
Gabriel McAdams
@Gabriel, not if you want to use it later on inside t-sql ... the `x=` and `as x` will just name the corresponding column with that name, while declaring the variable allows you direct access to it from within the same t-sql block...
Gaby
Which is what I said. `x =` and ` as x` both do the same thing. I only pointed out that there was no reason to believe that the OP wanted to use a variable. It looks to me like the poster only wants to use an alias - and `x =` is the same as ` as x` in that case.
Gabriel McAdams
+6  A: 

Use:

SELECT x = CASE 
             WHEN t.xvalue IN (52, 57, 82, 83) THEN 'xvalue'
             WHEN t.yvalue IN (01, 02, 11) THEN 'yvalue'
             ELSE NULL
           END
  FROM TABLE t

Assuming you want the value from the column, use:

SELECT x = CASE 
             WHEN t.xvalue IN (52, 57, 82, 83) THEN t.xvalue
             WHEN t.yvalue IN (01, 02, 11) THEN t.yvalue
             ELSE NULL
           END
  FROM TABLE t

You realize that if both xvalue and yvalue are in the groups, only the xvalue will be displayed?

OMG Ponies
upvote for correct answer :)
Broken Link
+1  A: 

Sure, if xvalue is a numeric column, just remove the brackets. Then it should work fine. The brackets tell the database server that something is a database object, so remove them so they are treated like a literal.

Mike Mooney
+1  A: 

On t-sql, [] identifiers are for quoting symbols like column names - you can have spaces and all sorts of odd characters in column names if you really want to. Use '' quotes to quote string literals.

ConcernedOfTunbridgeWells
A: 

The reason your query doesn't work is that it is written incorrectly. Look at this:

in ([01],[02],[11])  

Putting [] around your values means you want them treated as column names. If you remove the quotes, then that part will work. Also look at this:

then "xvalue" 

you need single quotes here.

else 'NULL'

Here, do you want the value to be NULL or the string "NULL"? The way you have written it, it will be the string "NULL"

To make it NULL value, write it like this:

else NULL

Here is the whole thing again, written correctly:

select    
x =   
case    
    when xvalue in (52,57,82,83)    
        then 'xvalue'
    when yvalue in (01,02,11)    
        then 'yvalue'
    else 'NULL'   
    end   
from xyztable
Gabriel McAdams