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

Conditionally setting a variable in SQL

I'm trying to do this: If the Day parameter is the current day, then set @EndDate to be yesterday instead If the Day parameter is in the future, set @EndDate to yesterday. I've tried a few different approaches including two down below. I'm…
Maddie
  • 37
  • 1
  • 4
1
vote
0 answers

Query with many CASE statements - optimization

Possible Duplicate: Query with many CASE statements - optimization Hi guys, 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…
Nemanja Vujacic
  • 925
  • 5
  • 14
  • 25
1
vote
2 answers

Case Statement Not Working - VBA

In my project I have a list of clients that have unique commission calculations (ex. Client_1 has a 0.1% commission rate, whereas Client_2 has a 0.25% commission rate). In my VBA code I made special cases, that vary by the client_id. The problem…
Nikolajs
  • 325
  • 1
  • 3
  • 17
1
vote
1 answer

Conditional join using case statement in 'on' clause in HiveQL

Let's say: the values in the column of tbl_A to join on have different lengths: 5 and 10. The values in the column of tbl_B to join on are larger length and when joining substr() should be applied depending on the length of the values in tble_A. So…
Lilu
  • 108
  • 8
1
vote
2 answers

How do I write a conditional Ruby case statement with parameters passed into the case?

I'm trying to figure out a way for a case statement to take into account two parameters passed in from a regex. My regex example is: When (/^(?:a|an|the) (RoleA|RoleB) attempts to access a (New|Updated) Record$/) do |role_type, record_type|. I have…
qaminded
  • 13
  • 3
1
vote
0 answers

UDF, Triggers, and IF/ELSE or CASE in PostgreSQL

I have a games table, that records p1_id, p1_score, p2_id, p2_score, and winner_id. I would like to have a trigger that uses a user defined function to automatically fill in the winner (as practice of using both). I have the trigger: CREATE TRIGGER…
glenrothes
  • 1,543
  • 1
  • 14
  • 17
1
vote
1 answer

Using CASE statement name as another reference filed in another CASE statement

Is it possible to refer to another case statement name in another case statement within SQL query? Example: I have 3 case statements. The first 2 case statements are returning values based off coded fields. My 3rd case statement I would like to…
Neisha
  • 13
  • 1
  • 4
1
vote
1 answer

Always encrypted - SQL Server 2016 How to user case statement on encrypted columns

i have encrypted the existing table columns using always encrypted feature in SQL server 2016. few of my existing queries are failing as we have used case statement in where clause, LEN and substring function. can someone tell me how to use these…
Windows10
  • 51
  • 9
1
vote
2 answers

About Cases and switches in c# do we really need them?

I just wrote a program with manny Cases and Switches. So in C# is there any way to avoid Cases and Switches? I ended up with kind of a "messy" program in my point of view. All the other senior full stack developer, they said it was good and so on.…
Andy
  • 37
  • 1
  • 4
1
vote
2 answers

CASE statement syntax error in SAP Lumira / Freehand SQL

I’m using SAP Lumira desktop and "Query with SQL (Freehand SQL)", connected to SAP ECC. I try to extend the query with a case statement but run into errors as below: SELECT "VBUK-UVALS", CASE ("VBUK-UVALS") WHEN 'A' THEN 'Closed' …
BB8
  • 11
  • 2
1
vote
0 answers

Avoid 'Out of Memory' error in Netezza by finding large CASE statement alternative

Below is a query that I've simplified quite substantially in the past week. It used to have 7 other joins, but now they are consolidated into two temp tables, so it's obviously not complex joins causing the error. But it still throws an 'Out of…
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
1
vote
3 answers

Finding values that matches max column in SQL server

I have a table that has the max months column like the one below: Category MaxMonths Title X 3 Beginner-1 X 6 Intermediate-1 X 12 Avance-1 X 999 Master-1 Y …
abahr
  • 33
  • 5
1
vote
1 answer

Create Field off Case Statement in Another Field

I'm trying to figure out a query that will help me to create an AgeBucket field based of the source code I'm pulling. Currently, I made a field called BusinessAge that basically calculates the number of business days from a start date of a given…
smul86
  • 401
  • 1
  • 8
  • 22
1
vote
3 answers

take Duplicated ID's out and Identify a new columns

I Joined 6 table together to gather all information that I need. I want all Id's, Names, Birthdays, and Ethnicity. Some Ids have 2 or more Ethnicity and that will cause a id be duplicated. I am thinking of writing a sub query or can I just use a…
1
vote
1 answer

Is it possible to put a select statement in a case statement?

I am currently trying to use a select statement inside my select statement. So far that works great. But i want it to select something different depending on some conditions. My problem is putting the SQL statement subquery in the THEN part of the…
Azuraith
  • 1,030
  • 14
  • 28