Questions tagged [case-statement]

A case statement allows sequences of SQL statements to be selected for execution based on search or comparison criteria, and is typically used in stored procedures. Do not use this tag, use [switch-statement] instead.

The CASE statement provides a mechanism for conditional execution of SQL statements.

It exists in two forms: the simple case and the searched case.

The simple case involves an equality comparison between one expression and a number of alternative expressions, each following a WHEN clause.

The searched case involves the evaluation for truth of a number of alternative search conditions, each following a WHEN clause.

In each form of the CASE it is the first WHEN clause to evaluate to true, working from the top down, that determines which sequence of SQL statements will be executed.

There may be one or more SQL statements following the THEN clause for each WHEN. If none of the WHEN clauses evaluates to true, the SQL statements following the ELSE clause are executed. If none of the WHEN clauses evaluates to true and there is no ELSE clause, an exception condition is raised to indicate that a case was not found.

Providing an ELSE clause supporting an empty compound statement will avoid an exception condition being raised, in cases where no ‘else’ action is required, when none of the WHEN alternatives evaluates to true.

430 questions
4
votes
2 answers

EF Left joining a table on two properties combined with a case statement

I'm trying to write a query for a database that will left join a table to a look up table and the results will be returned based on a case statement. In normal SQL the query would look like this: SELECT chis_id, chis_detail, cilt.mhcatID,…
4
votes
2 answers

using case statement in a where clause

Hello I am missing something because my code errors. select * from ##ScheduleDetail SD left join ##HolidayFilterTbl HF on SD.Scheduledate = HF.Testdate where (ScheduleDate = testdate) and (Case when HF.IsHoliday = 1 then (overtime = 1 and makeup…
Comicwizard
  • 55
  • 1
  • 1
  • 3
4
votes
2 answers

mysql switch case

I have a query structure like below, Im wondering if there is a way to the write the select queries as one using CASE statements or by some other means so that the values get inserted into the appropriate variables based on their values. DECLARE…
volting
  • 16,773
  • 7
  • 36
  • 54
4
votes
3 answers

Query with many CASE statements - optimization

I have one very dirty query that per sure can be optimized because there are so many CASE statements in it! SELECT (CASE pa.KplusTable_Id WHEN 1 THEN sp.sp_id WHEN 2 THEN fw.fw_id WHEN 3 THEN s.sw_Id WHEN 4 THEN…
Nemanja Vujacic
  • 925
  • 5
  • 14
  • 25
4
votes
2 answers

PL/SQL Using CASE in WHERE clause

Good day Stackoverflow! I have a query that is giving me an error: "Missing Right Parenthesis", at least, so says SQL Developer. My query has a CASE statement within the WHERE clause that takes a parameter, and then executing a condition based on…
P.M.
  • 53
  • 1
  • 1
  • 7
4
votes
1 answer

How to reference a val in a case statement?

I'm having a slow morning. I thought referencing an existing val in a case statement would be OK. But it seems it is interpreted as a local variable definition. A rudimentary googling didn't help and I don't have my staircase book with me. In the…
Synesso
  • 37,610
  • 35
  • 136
  • 207
4
votes
1 answer

MySQL Joins With Case Statements

I am having a bit of trouble with a query. I want to join tables if a case is met. This the query I'm working with. I'm kind of new to these case statements so any help is greatly appreciated! SELECT conversation.c_id, …
Brian Moreno
  • 977
  • 4
  • 11
  • 39
4
votes
1 answer

Ruby - Implicit object of a case statement

In Ruby, is there a way to get the implicit object of a case statement? case 2+2 when '2' puts '2' else puts "#{some_object}" end Where 'some_object' would be the return value of whatever statement was evaluated by case
Ethan Gunderson
  • 10,959
  • 8
  • 30
  • 29
4
votes
3 answers

How do you test inequality with Oracle Case Statement

This works fine: select case (1+2) -- (or_some_more_complicated_formula_yielding_a_numeric_result) when 200 then '200' when 100 then '100' else 'other' end hi_med_low from dual ; But I need to…
AWhatley
  • 219
  • 1
  • 2
  • 11
4
votes
4 answers

Haskell - Maybe arithmetic

I have been asked to implement a function which uses the following profile: maybe_divide :: Maybe Integer -> Maybe Integer -> Maybe Integer and responds in the following manner: > maybe_divide (Just 5) (Just 2) Just 2 > maybe_divide (Just (-5))…
4
votes
1 answer

IF and Case statements in MySQL

I would like to select * if 5 is greater than 2, if false select a particular column. Where am I going wrong? SELECT IF(5>2, *, column_x), CASE whereheard_name WHEN 'Newspaper' THEN 'a' WHEN 'TV' THEN 'b' WHEN 'Internet' THEN 'c' …
Tommy02
  • 41
  • 1
  • 5
4
votes
3 answers

Nested switch? Or other solution checking different conditions?

Okay, the situation is as following: I have one client that has 2 settings: ConnectionState and ConnectionSollState, both the same enumerable (TypeConnectionState), they store the actual state of the client connection and the state the connection…
3
votes
4 answers

SQL - Creating A Conditional Where Clause For This Simple Query

I've been trying to create a conditional where clause for my query below but I keep seeing so many alternatives I'm not sure what to use in this case. What I need is something along the lines of this: (though of course this code is wrong) where…
TJH
  • 189
  • 1
  • 5
  • 18
3
votes
2 answers

Dynamic case when

SELECT COUNT(id), AgeRange FROM ( select id, case when age < 0 then 'less than 0' when age >= 0 and age <=30 then '0-30' when age >= 31 and age <=60 then '31-60' when age >= 61…
Pramod
  • 657
  • 4
  • 18
  • 34
3
votes
2 answers

Can we use regular expressions In Bash case statements?

I was digging through mysql_safe (trying to add some options) and I came across this bash scripting technique they use to assign variables from the Launch Agent: com.mysql.mysqld.plist (I'm on a mac). Now mysqld_safe doesn't know it's being invoked…
Ashley Raiteri
  • 700
  • 8
  • 17
1 2
3
28 29