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

CASE statement results for multiple fields

Running PostgreSQL 7.x (Yeah I'm upgrading) Example: SELECT CASE WHEN "substring"(t."Field"::text, 0, 3) = '01'::text THEN 'group one'::text WHEN "substring"(t."Field"::text, 0, 4) = '123'::text THEN 'group one'::text …
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
1
vote
5 answers

Sql Server: CASE Statement does unexpected behavior when comparing to NULL

Given: The following Select statement: select case NULL when NULL then 0 else 1 end Problem: I'm expecting this to return 0 but instead it returns 1. What gives?
aarona
  • 35,986
  • 41
  • 138
  • 186
1
vote
2 answers

Issue with case statement on VBA

I am very new to VBA and I having an issue with a Case Statement I am trying to write. Overview of what I want the code to do I need to assign different fee rates based on two criteria: the risk profile and value. If the risk profile is Foreign…
Nini
  • 13
  • 3
1
vote
1 answer

Non-synthesizable clocking style: only conditional operations or if statements are supported in sync-async always blocks

I am trying SV code which involves both using priority and casez constructs together. What I want to achieve is based on the valid request available (sel is a 4-bit register which will contain who has valid request at a time). An output will be…
1
vote
1 answer

Oracle SQL Listagg remove duplicates with case statement conditions

I am trying to show repeated column values with comma separated list by using listagg but getting error as "Not a single group by function". Hope I get some help. Below is the DDL script with insert statements and data: DROP TABLE dept CASCADE…
Richa
  • 337
  • 4
  • 18
1
vote
1 answer

MySQL if then/ case statement

there! I'm writing mysql script in mySQl Front 4.1. I have problem with if then, case statements. I have next code: set @prodID = -1; select @prodID = productID from partid_to_productid where PartID= 8; case @prodID WHEN NULL then select 0; …
Roman
  • 565
  • 1
  • 10
  • 27
1
vote
1 answer

Combined Column in Google Data Studio

I am having trouble combining separate columns with null values into one. The example table I have contains values like so Col A Col B Col C null 100 null 55 null null null null 27 I want to write the statement such that I get a…
Deca
  • 11
  • 5
1
vote
8 answers

Ways to speed up a huge case statement? C++

I am running through a file and dealing with 30 or so different fragment types. So every time, I read in a fragment and compare it's type (in hex) with those of the fragments I know. Is this fast or is there another way I can do this quicker? Here…
Scrimshaw Rest
  • 183
  • 1
  • 1
  • 4
1
vote
1 answer

SQL: IF/CASE statement within WHERE clause

Is it possible to return records matching the first part of a where clause; however, if no results are found, then move to the second part of the where clause? Example: create table #Fruits ( Fruit varchar(20), Color varchar(20), Size…
lumiukko
  • 249
  • 3
  • 13
1
vote
2 answers

how to use missing and boolean values in the same case statement in excel vba

In VBA (MS Excel 2016): How to combine missing and boolean values in a select case statement? The argument val could be passed as a boolean or not passed at all, or passed as something else unexpected. public sub test(optional val as variant) …
ciso
  • 2,887
  • 6
  • 33
  • 58
1
vote
2 answers

How it works in case of using case statement inside min function in postgresql?

I am junior web developer. I am studying about postgres, and I have a question about use case of case statement. This query finds several rows with data that most closely matches a particular word. The result can be multiple. In this query, the…
kk jj
  • 13
  • 4
1
vote
1 answer

Combining two separate case statements in NetSuite Case Statements

I currently have two separate saved search columns for these two statements, and I'm hoping to combine them and list the data in just one column. I've tried OR AND, but the results then error out. First Statement Case when {item.custitem54} is null…
Imran
  • 93
  • 2
  • 15
1
vote
2 answers

Use column names as values for Phone Number

I have a table in which has phone numbers split out into separate columns cellphone, homephone, workphone and otherphone. I would like to do some sort of transformation which pulls all the phones into one column but shows the phone relationship in…
an1234
  • 165
  • 1
  • 12
1
vote
2 answers

Is there more elegant way to write statement with overlapping (nested) alternatives?

I want to write case statement with overlapping alternatives. Quick search on the internet gives me examples with alternatives which is unique and not overlapping, smth like: case $var in a) ;; b) ;; *) ;; esac But…
ivoriik
  • 155
  • 1
  • 11
1
vote
1 answer

highlight results when item fulfillment date is not same as invoice date

I have saved search which displays invoices when the invoice date is not same as the ship date.I now only need to highlight records when the dates are in different months. For example I already have a saved search list to show invoice if the invoice…
Imran
  • 93
  • 2
  • 15