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
1
vote
2 answers

Case Statement Not Assigning Value

I'm having some trouble debugging a case statement. I was hoping that the statement would assign numeric values to note-val, but so far it is assigning #. I know it's something wrong with the case statement, because if I add an else clause,…
Andy
  • 3,132
  • 4
  • 36
  • 68
1
vote
1 answer

Error occuring on sum case statement using a date between

I am trying to create a statement which will retrieve some stats from the the database. I am trying to use a SUM case statement to retrieve the number of records which have a join date between start and end of the previous year, however I keep…
1
vote
1 answer

REGEXP_MATCH multiple words in a string using CASE statement in Google DataStudio

I am using Google Datastudio to make a CASE statement to take a multi-words string and split it out into categories. I was asked to use REGEXP_MATCH (nothing else, I know contains function would be easier). I need a solution to match the following…
1
vote
0 answers

Custom SQL in Tableau, CASE-created fields

I am executing a New Custom SQL with CASE-choice fields in Tableau: SELECT x1, x2, x3 ..., CASE WHEN x1>1 THEN 'y' ELSE '' END AS x_new ... FROM table1a FULL JOIN table2a... WHERE x3>4 UNION ALL SELECT x1, x2, x3 ..., CASE…
casey
  • 11
  • 3
1
vote
1 answer

handle cases of Enums in Swift

How can we handle the same case with different inputs from the response in Enum's enum MyNotificationType: String, Codable { case practice = "push" case practice = "PracticeRecommendation" case play = "PlayRecommendation" …
Lion
  • 872
  • 1
  • 17
  • 43
1
vote
3 answers

Is there a SQL update query to update the same value for multiple records within the same user id?

I have a column that is used for calculations only. It displays either a 0 or a positive number which represents vacation time. In our Time table it is entered as a negative number and I convert it to a positive number for calculations. I need that…
mcedave
  • 15
  • 6
1
vote
3 answers

SQL CASE Statement and Multiple Conditions

I am writing a basic SQL query to build a table in a cloud-based reporting tool, which supports a wide range of SQL "dialects" (no server-side access to the database). I have a working knowledge of SQL and it's needed to be able to construct a basic…
wyattburp86
  • 51
  • 1
  • 8
1
vote
0 answers

Tableau - multiple columns with True and False values

I have 10 columns with True and False values. I would like to create a dashboard where if one column is True and another column is also True, a graph displays a count in that column. For example: if column A is True, column B if False, column C is…
1
vote
3 answers

Oracle: Using CASE statement variable in an operation

I have the following SQL query: SELECT PLAYER_ID, PLAYER_CD, TEAM_ID, USR_FNM, USR_MNM, USR_LNM, CASE WHEN TEAM_ID = '11111111111111' THEN SIGN_DT ELSE START_DT END AS…
samg
  • 311
  • 1
  • 8
  • 21
1
vote
1 answer

Is it possible to repeat when clause in case statement based on number of variables?

As a part of my work I need to create profiles of claims settlement. Here is a quick idea about my work. We receive claims data. I need to create report of profiles of claims where we mention range of claim amount and corresponding number of claims…
Bunny
  • 43
  • 1
  • 10
1
vote
2 answers

A simpler list of cases

I have to test many cases, but this solution is not very elegant: if '22' in name: x = 'this' elif '35' in name: x = 'that' elif '2' in name: # this case should be tested *after* the first one x = 'another' elif '5' in name: x =…
Basj
  • 41,386
  • 99
  • 383
  • 673
1
vote
1 answer

Conditional WHERE statement to show a certain Fiscal Week Number

Looking for some guidance on how to best execute this code... Ultimately, I want to insert this into the where clause so that the FSCL_YR_WK (i.e. 201804) will be driven based on the current date. AND A.FSCL_YR_WK = Case WHEN…
1
vote
1 answer

SQR CASE STATEMENT GIVING ERROR WHEN LOCAL VARIABLE IS USED

I need to use a date variable in a case statement for the select section of a sql statement in sqr and keep getting an error. It works fine when i use the getdate() function to compare but doesn't work with my variable. help please. code is…
1
vote
0 answers

VHDL: Using case statements for FSM with two outputs

I'm new to VHDL and I've been trying to finish my combinatorial code in an assignment, however I don't know how to make cases account for specific combinations of the two 1-bit outputs: Permit and ReturnChange. My concern is is with the WHEN…
Eric
  • 117
  • 1
  • 2
  • 12
1
vote
2 answers

case statement in Spark SQL

I am working on a workflow for my company. Therefore I need to use a Spark SQL case-statement to filter something. I have a column called OPP_amount_euro (the amount of money used for something is saved there) and I have a column called…
adama
  • 537
  • 2
  • 10
  • 29