Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL - Case - Values in?

Tags:

People also ask

Can we use case inside case in SQL?

CASE can be nested in another CASE as well as in another IF…ELSE statement. In addition to SELECT, CASE can be used with another SQL clause like UPDATE, ORDER BY.

How do you use variables in a case statement in SQL?

For example, if I want to check the values of two variables, or use any kind of comparison other than equality, I can do this: SELECT CASE WHEN @a > 5 OR @b <= 10 THEN c + @a - @b ELSE c - @b + @a END FROM dbo. table; In either case, the result is always a single expression.

Can we use CASE statement in where clause in SQL Server?

CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.


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