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
129
votes
4 answers

Guards vs. if-then-else vs. cases in Haskell

I have three functions that find the nth element of a list: nthElement :: [a] -> Int -> Maybe a nthElement [] a = Nothing nthElement (x:xs) a | a <= 0 = Nothing | a == 1 = Just x | a > 1 = nthElement xs…
nucleartide
  • 3,888
  • 4
  • 28
  • 29
110
votes
3 answers

How do I test for an empty string in a Bash case statement?

I have a Bash script that performs actions based on the value of a variable. The general syntax of the case statement is: case ${command} in start) do_start ;; stop) do_stop ;; config) do_config ;; *) do_help ;; esac I'd like to…
Singlestone
  • 2,019
  • 3
  • 16
  • 18
108
votes
2 answers

SQL Server IIF vs CASE

I recently came to know about the availability of IIF function in SQL Server 2012. I always use nested CASE in my queries. I want to know the exact purpose of the IIF statement and when should we prefer using IIF over CASE Statement in the query. I…
neophyte
  • 1,726
  • 3
  • 15
  • 21
106
votes
16 answers

Case Statement Equivalent in R

I have a variable in a dataframe where one of the fields typically has 7-8 values. I want to collpase them 3 or 4 new categories within a new variable within the dataframe. What is the best approach? I would use a CASE statement if I were in a…
Btibert3
  • 38,798
  • 44
  • 129
  • 168
104
votes
16 answers

Why do we need break after case statements?

Why doesn't the compiler automatically put break statements after each code block in the switch? Is it for historical reasons? When would you want multiple code blocks to execute?
unj2
  • 52,135
  • 87
  • 247
  • 375
102
votes
3 answers

MySQL select statement with CASE or IF ELSEIF? Not sure how to get the result

I have a two tables. One has manufacturer information and includes the regions where they can sell. The other has their products for sale. We have to limit visibility of the product based on the regions. This is like Netflix have videos in their…
user409858
  • 1,121
  • 2
  • 8
  • 13
84
votes
6 answers

How do I use properly CASE..WHEN in MySQL

Here is a demo query, notice it is very simple, Fetches only where base_price is 0, And still, it chooses the condition 3: SELECT CASE course_enrollment_settings.base_price WHEN course_enrollment_settings.base_price = 0 THEN 1 WHEN…
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
81
votes
5 answers

Conditional JOIN Statement SQL Server

Is it possible to do the following: IF [a] = 1234 THEN JOIN ON TableA ELSE JOIN ON TableB If so, what is the correct syntax?
J.S. Orris
  • 4,653
  • 12
  • 49
  • 89
73
votes
11 answers

Regarding Java switch statements - using return and omitting breaks in each case

Given this method, does this represent some egregious stylistic or semantic faux pas: private double translateSlider(int sliderVal) { switch (sliderVal) { case 0: return 1.0; case 1: return .9; …
NickAbbey
  • 1,201
  • 2
  • 10
  • 17
69
votes
3 answers

How does MySQL CASE work?

I know that SQL's CASE syntax is as follows: CASE WHEN search_condition THEN statement_list [WHEN search_condition THEN statement_list] ... [ELSE statement_list] END CASE However, I don't understand how this works, possibly because I'm…
JD Isaacks
  • 56,088
  • 93
  • 276
  • 422
67
votes
6 answers

Case statement in MySQL

I have a database table called 'tbl_transaction' with the following definition: id INT(11) Primary Key action_type ENUM('Expense', 'Income') action_heading VARCHAR (255) action_amount FLOAT I would like to generate two columns: Income Amt and…
Pradip Kharbuja
  • 3,442
  • 6
  • 29
  • 50
65
votes
9 answers

Can I use a case/switch statement with two variables?

I am a newbie when it comes to JavaScript and it was my understanding that using one SWITCH/CASE statements is faster than a whole bunch of IF statements. However, I want to use a SWITCH/CASE statement with two variables. My web app has two…
user918967
  • 2,049
  • 4
  • 28
  • 43
64
votes
5 answers

GROUP BY + CASE statement

I have a working query that is grouping data by hardware model and a result, but the problem is there are many "results". I have tried to reduce that down to "if result = 0 then keep as 0, else set it to 1". This generally works, but I end up…
ssbsts
  • 844
  • 1
  • 8
  • 13
63
votes
5 answers

SQL changing a value to upper or lower case

How do you make a field in a sql select statement all upper or lower case? Example: select firstname from Person How do I make firstname always return upper case and likewise always return lower case?
Joshua Hudson
  • 2,187
  • 2
  • 20
  • 24
62
votes
7 answers

Regular expressions in a Bash case statement

I am using following script, which uses case statement to find the server. #!/bin/bash SERVER=$1; echo $SERVER | egrep "ws-[0-9]+\.host\.com"; case $SERVER in ws-[0-9]+\.host\.com) echo "Web Server" ;; db-[0-9]+\.host\.com) echo "DB…
Unni
  • 1,721
  • 2
  • 14
  • 12