Questions tagged [case]

In SQL, the CASE expression evaluates a list of conditions, returning the result for the first condition which evaluates to true. For programming languages' CASE questions, use the [switch-statement] tag instead.

The CASE expression is an SQL construction used to return different values depending on specified conditions. There are two forms of the CASE expression. The first is the simple CASE:

CASE search_expr
WHEN compare_expr1 THEN result_expr1
WHEN compare_expr2 THEN result_expr2
ELSE else_expr
END

This will compare search_expr to each compare_expr in order until it finds a condition where search_expr = compar_expr, and return the result_expr of that condition. If no such condition is found the else_expr is returned.

The second form of CASE expression is the searched CASE:

CASE 
WHEN boolean_expr1 THEN result_expr1
WHEN boolean_expr2 THEN result_expr2
ELSE else_expr
END

This will test boolean_expr in order until it finds a condition where the boolean_expr is true, and return the result_expr of that condition. If no such condition is found the else_expr is returned.

There are two short forms for special CASE expressions; COALESCE and NULLIF.

COALESCE(x1, x2, ..., xn) 

is equivalent to:

CASE WHEN x1 IS NOT NULL THEN x1 
     WHEN x2 IS NOT NULL THEN x2
     ...
     ELSE xn
END 

I.e. the COALESCE expression returns the value of the first non-null operand, found by working from left to right, or null if all the operands equal null.

NULLIF(x1, x2) 

is equivalent to

CASE WHEN x1 = x2 THEN NULL ELSE x1 END

I.e. if the operands are equal, NULLIF returns null, otherwise the value of the first operand.

8090 questions
1775
votes
29 answers

How do I perform an IF...THEN in an SQL SELECT?

How do I perform an IF...THEN in an SQL SELECT statement? For example: SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product
Eric Labashosky
  • 29,484
  • 14
  • 39
  • 32
789
votes
2 answers

What is the Python equivalent for a case/switch statement?

Is there a Python equivalent for the switch statement?
John Alley
  • 8,067
  • 3
  • 14
  • 10
601
votes
11 answers

OR is not supported with CASE Statement in SQL Server

The OR operator in the WHEN clause of a CASE statement is not supported. How can I do this? CASE ebv.db_no WHEN 22978 OR 23218 OR 23219 THEN 'WECS 9500' ELSE 'WECS 9520' END as wecs_system
Werner
  • 6,125
  • 4
  • 17
  • 11
277
votes
7 answers

PostgreSQL Crosstab Query

Does any one know how to create crosstab queries in PostgreSQL? For example I have the following table: Section Status Count A Active 1 A Inactive 2 B Active 4 B Inactive 5 I would like the query to…
user151419
262
votes
12 answers

How to use a switch case 'or' in PHP

Is there a way of using an 'OR' operator or equivalent in a PHP switch? For example, something like this: switch ($value) { case 1 || 2: echo 'the value is either 1 or 2'; break; }
alex
203
votes
4 answers

SQL Server: converting UniqueIdentifier to string in a case statement

We have a log table that has a message column that sometimes has an exception stack trace. I have some criteria that determines if the message has this. We do not want to show these messages to the customer but instead have a message like: Internal…
aarona
  • 35,986
  • 41
  • 138
  • 186
194
votes
2 answers

PostgreSQL CASE ... END with multiple conditions

Here is an extract of my table: gid | datepose | pvc ---------+----------------+------------ 1 | 1961 | 01 2 | 1949 | 3 | 1990 | 02 1 | 1981 | 1 | …
wiltomap
  • 3,933
  • 8
  • 37
  • 54
192
votes
10 answers

Using Case/Switch and GetType to determine the object

Possible Duplicate: C# - Is there a better alternative than this to ‘switch on type’? If you want to switch on a type of object, what is the best way to do this? Code snippet private int GetNodeType(NodeDTO node) { switch (node.GetType()) …
user29964
  • 15,740
  • 21
  • 56
  • 63
191
votes
11 answers

Can I use CASE statement in a JOIN condition?

The following image is a part of Microsoft SQL Server 2008 R2 System Views. From the image we can see that the relationship between sys.partitions and sys.allocation_units depends on the value of sys.allocation_units.type. So to join them together I…
Just a learner
  • 26,690
  • 50
  • 155
  • 234
179
votes
14 answers

SQL Switch/Case in 'where' clause

I tried searching around, but I couldn't find anything that would help me out. I'm trying to do this in SQL: declare @locationType varchar(50); declare @locationID int; SELECT column1, column2 FROM viewWhatever WHERE CASE @locationType WHEN…
Miles
  • 5,646
  • 18
  • 62
  • 86
161
votes
3 answers

Case in Select Statement

I have an SQL statement that has a CASE from SELECT and I just can't get it right. Can you guys show me an example of CASE where the cases are the conditions and the results are from the cases. For example: Select xxx, yyy case : desc case…
fadzli feizal
  • 1,617
  • 2
  • 12
  • 8
155
votes
8 answers

How can I have lowercase routes in ASP.NET MVC?

How can I have lowercase, plus underscore if possible, routes in ASP.NET MVC? So that I would have /dinners/details/2 call DinnersController.Details(2) and, if possible, /dinners/more_details/2 call DinnersController.MoreDetails(2)? All this while…
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
137
votes
14 answers

"CASE" statement within "WHERE" clause in SQL Server 2008

I am working with a query which contains "CASE" statement within "WHERE" clause. But SQL Server 2008 is giving some errors while executing it. Can anyone please help me with the correct query? Here is the query: SELECT tl.storenum 'Store #', …
user1018213
  • 2,095
  • 4
  • 16
  • 10
132
votes
6 answers

T-SQL: Using a CASE in an UPDATE statement to update certain columns depending on a condition

I am wondering if this is possible at all. I want to update column x if a condition is true, otherwise column y would be updated UPDATE table SET (CASE (CONDITION) WHEN TRUE THEN columnx ELSE columny END) = 25 I…
pqsk
  • 2,124
  • 3
  • 23
  • 28
130
votes
6 answers

What are the different kinds of cases?

I'm interested in the different kinds of identifier cases, and what people call them. Do you know of any additions to this list, or other alternative names? myIdentifier : Camel case (e.g. in java variable names) MyIdentifier : Capital camel case…
Austin Cory Bart
  • 2,159
  • 2
  • 19
  • 32
1
2 3
99 100