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
2
votes
1 answer

How to refactor case statement to avoid Error: undefined method `[]' for nil:NilClass when runnig system test?

I'm learning Ruby-on-Rails and doing some homework from school. I need to refactor the default_avatar method. The logic that I want to accomplish is: First, check whether the user name is present, and return the default avatar (AVATAR_CATLIKE in…
2
votes
4 answers

Using two = signs in CASE statement?

I'm attempting to use a case statement, dependent on a variable, to determine part of my WHERE clause, but it doesn't like what I'm doing. Here's my query (modified for simplicity) DECLARE @SalesType VARCHAR(10) SET @SalesType = 'Bulk' SELECT CASE…
jw11432
  • 545
  • 2
  • 20
2
votes
2 answers

Python Pandas equivalent for SQL case statement using lead and lag window function

New to Python here and trying to see if there is a more elegant solution. I have a time series data of telematics devices that has motion indicator. I need to expand the motion indicator to +/- 1 row of the actual motion start and stop (denoted by…
nikeshpraj
  • 53
  • 1
  • 6
2
votes
1 answer

Is it acceptable to use a switch statement within a switch statement?

I know my documentation is not superb. This program is to create a Rock, Paper, Scissors Game: I initially struggled with getting the case statements withing case statements to work properly but I feel like I resolved the issue. My question: is…
Rye Guy
  • 45
  • 2
2
votes
1 answer

Grouping case expressions in elm (0.18)

Is there a way in elm (0.18) to group together a series of case expressions that do the same thing? For example: type Character = Sleepy | Happy | Grumpy | Dopey | Sneezy | Bashful | Doc | SnowWhite |…
Mark Karavan
  • 2,654
  • 1
  • 18
  • 38
2
votes
2 answers

Compiler optimizations on case statement

I would like to broaden my knowledge and skills in compiler writing, especially optimizations. I would like to know what optimizations are available for case-statements with case expression of string type. For instance in Object…
LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
2
votes
1 answer

Elixir case statement and pattern matching

I am trying to paint a room with walls into a map of %{{x, y} => [items]} and I am using a case statement to figure out what type of wall I want to draw. However it sems to try to do pattern matching and assign pos to the values on left. (throwing…
hakunin
  • 4,041
  • 6
  • 40
  • 57
2
votes
2 answers

Case Syntax Errors

Hello I am attempting to do this with mySQL. I tried using SELECT IF and CASE to alter titles. Every title description has "A " in front of the description; even if the second word starts with a consonant. So I'm trying to query the descriptions but…
2
votes
3 answers

Case within join's "ON" caluse

I'm trying to do something like this... SELECT lc.Location, count(h.empID) as 'Count', lu.LBL FROM swam.lookup lu LEFT JOIN swam.empTable h ON CASE WHEN lu.sac is null THEN lu.POS = h.POS ELSE ( …
blacksaibot
  • 265
  • 2
  • 12
2
votes
2 answers

MySQL - Stored Procedure - Using a Case Expression inside Case Statement?

I'm currently in the process of writing a stored procedure that uses case statements to determine what queries to run. Inside one of my queries is a case expression and I get a syntax error every time I try to save the altered procedure. The…
2
votes
3 answers

Getting the number of cases in a switch-case in C

Is it possible to get the number of cases in a switch case in C without manually adding a counter variable which is incremented in each case?
floquet22
  • 323
  • 2
  • 15
2
votes
2 answers

Trying to set a variable inside a case statement.

I'm trying to update a date dimension table from the accounting years table of our ERP System. If I run the following Query: SELECT fcname FYName ,min(fdstart) YearStart ,max(fdend) YearEnd ,max(fnnumber) PeriodCount FROM…
DavidStein
  • 3,149
  • 18
  • 41
  • 62
2
votes
2 answers

Shell script read file path until file exisits

The user inputs the file path and the shell script must check for file existence and if the file doesn't exist, prompt for correct path until the correct path is provided. Here is my attempt and I don't like it if [[ -z $filepath || ! -f $filepath …
Ali
  • 7,810
  • 12
  • 42
  • 65
2
votes
1 answer

Is there any alternative of case statement?

I have a situation where if the column value is less the 1 then I have to show NO as column value, else YES. I am using CASE statement for this. But after applying CASE statement query is taking to much time to execute. Is there any alternate way…
gkarya42
  • 429
  • 6
  • 22
2
votes
2 answers

Multiple Case Statements to one row

In my SQL Server database one customer could have many products. When casing using this: CASE WHEN br.Ptype# = 'LE' THEN 'Y' ELSE 'N' END AS [Legal], CASE WHEN br.Ptype# = 'BR' THEN 'Y' ELSE 'N' END AS [BR], CASE WHEN br.Ptype#…
Ramo
  • 21
  • 1
  • 4