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
3
votes
5 answers

Simple Lisp Case statement question - problem comparing to nil

I'm trying to use a case statement to make some code more readable. It seems to work as a series of if statements, but for some reason the case statement always accepts a comparison to nil even if it is not true. Can someone clarify why this…
Alex Pritchard
  • 4,260
  • 5
  • 33
  • 48
3
votes
1 answer

Why can't I use enums for switch-case-statements?

I want to use enum-constants for switch-case-statements. I am using following enum/class: public enum Cons { ONE(1), TWO(2); private final int val; private Cons(final int newVal) { val = newVal; } public int getVal() { …
Naboki
  • 85
  • 1
  • 5
3
votes
3 answers

Alternative for case when statement in ruby

I am assigning a set of values based on the values of a hash. I have to assign values to another hash key based on the value of the incoming hash key. There are more than 10 keys in the incoming hash(so more than 10 cases). I want to minimize the…
3
votes
0 answers

Delphi "case of" expression expects constant

I was porting my communication library written in C to Delphi which uses #define and "switch case" for one of the function. I have translated them by using "const" and "case of" in Delphi. But Delphi compiler complains by saying that "[dcc32…
Mehmet Fide
  • 1,643
  • 1
  • 20
  • 35
3
votes
1 answer

Updating multiple variables in case statement

I've been looking around for a while so forgive me if maybe I'm using improper terminology... The goal of the code is to update Aout1 and Aout0 if the input is 0, with the output corresponding to a 7-segment display, but I'm getting the following…
gdagis
  • 113
  • 1
  • 4
  • 15
3
votes
1 answer

SQL Server T-SQL Replace

I have a case statement where I am trying to change two values from a field. CASE WHEN prod_map.Product_ID1 = 'CR' THEN REPLACE(REPLACE(cl.trade_day_count, 'ACT','ACTUAL'),cl.trade_day_count, 'ACT+1', 'ACTUAL') END, so, when cl.trade_day_count…
Iasha
  • 77
  • 1
  • 8
3
votes
1 answer

Does the following Case statement make sense?

The case statement below should be retrieving data whenever CL.TYPE = DIAG.TYPE Then DIAG.TYPE is true and if not not then NULL. Does this make sense? Or Is this the most logical way to do this? (CASE WHEN CL.TYPE2 = DIAG.TYPE OR CL.TYPE3 =…
EkansDasnakE
  • 67
  • 1
  • 10
3
votes
1 answer

CASE Statement with a SUM

This is my table structure. =================================== main_section | currency | amount =================================== Tender INR 2000 Bank USD 3000 Tender INR 1500 Tender …
Sanju Menon
  • 747
  • 1
  • 10
  • 24
3
votes
1 answer

Get wildcard value from bash case statement

I am wondering how to get the value from a case wildcard? I have an array that generates a menu for me. I then have a case that determines which option is chosen. The last part of the case statement is the wildcard value. I am looking to get the…
Ascalonian
  • 14,409
  • 18
  • 71
  • 103
3
votes
0 answers

CASE statement not working after switching to Oracle Provider for OLE DB

I am currently migrating SSIS packages from VS2008 to VS2012 to run on a 64-bit server and have come across an issue I can't seem to find a resolution to online. We have several Data Flow tasks that connect and pull data from different Oracle…
peppep1101
  • 31
  • 2
3
votes
3 answers

Ada case statement with strings

I'm trying to use a string in a case statement, however it is giving me expected a discrete type. Found type Standard.String I understand that strings are not discrete. I'm wondering if there is a work around or not. Here is my code: function…
NerdyFool
  • 531
  • 1
  • 7
  • 14
3
votes
2 answers

How to restart or reuse a case statement in Ruby?

After going through the codecademy ruby section "A Night at the Movies", I wanted to extend the case-statement to allow input again. By the end my code was: movies = { living_torah: 5, ushpizin: 5 } def input #method for gets.chomp …
3
votes
4 answers

Dealing with combining cases & duplicate cases in switch statements

Is it okay to combine cases that share assignments and repeat the case for assignments that are not shared, or is it preferred to just keep each separated? To illustrate with a simple example.. case 0 and 180 both include w = 330 so they have been…
davidcondrey
  • 34,416
  • 17
  • 114
  • 136
3
votes
2 answers

Filtering stored procedure records by nested select case statement

I need to further refine my stored proc resultset from this post, I need to filter my resultset to display only records where emailaddr is NULL (meaning display only records that have Invoice_DeliveryType value of 'N' ). Among numerous queries, I…
user3929962
  • 517
  • 3
  • 6
  • 23
3
votes
4 answers

LIKE operator for Progress DB SQL

I'm trying to do something like this in Progress SQL (THIS IS NOT POSTGRES!) SELECT CASE WHEN code LIKE '%foo%' THEN 'Y' ELSE 'N' END as foo FROM bar However Progress does not support a LIKE operator. INSTR looks like it might do the job,…
Tarski
  • 5,360
  • 4
  • 38
  • 47