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

using the case statement to find an index and return it

After playing about with this all morning(i am still new to this), I have decided to ask the experts. This is the quest: You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return…
godhar
  • 1,128
  • 1
  • 14
  • 34
1
vote
1 answer

Is there a component in talend for switch cases?

I have 100 when-then cases.I am looking for a component in Talend data tool which will help me putting this cases in a simple way. I have tried ternary operator in tmap and also if-else code in tJava. Anything else.
Ankush Rathi
  • 622
  • 1
  • 6
  • 26
1
vote
1 answer

SQL Server Select query validations

I have a MS SQL query on which I am using CASE STATEMENT. Like below - SELECT NAME, EMAIL, CASE Type WHEN LEN(Type) > 1 THEN 'LENGHT EXCEED' WHEN '' THEN 'Type is required' WHEN NULL THEN…
Pawan Agrawal
  • 263
  • 2
  • 4
  • 11
1
vote
2 answers

Using If else Or Case from More Table In SQL Server

I need to select all from Log Table and import data when Case If UserGrade =1 this is Admin User and I need to select Admin Name by Log.User If UserGrade =2 this is Teacher User and I need to select Teacher Name by Log.User If UserGrade =3 this is…
1
vote
2 answers

How do I exclude certain cases in a switch case statement?

option="${1}" case ${option} in 1) do A do B ;; 2) do A do B do C ;; 3) do A do B do C …
Shruti
  • 13
  • 4
1
vote
0 answers

TSQL Case statement that checks for non matches on two columns doesn't work on second column

This is baffling me. I'm trying to use a case statement to create a column that returns either a one or a zero depending on whether a search of two columns for a hospital site/ward IS NOT in a list of values in a table/variable, and if two columns…
Phteven
  • 139
  • 1
  • 13
1
vote
2 answers

Case statement won't match text from spreadsheet column in Google Apps script (script inside)

I have a google form and the corresponding results spreadsheet. I have a google apps script on the sheet that is intended activate on form submit. It reads the entry, puts formats the filled in columns into an email, and sends that email out to a…
Brian King
  • 11
  • 1
  • 4
1
vote
2 answers

Numeric Comparison of DataGridView Cell Value Evaluates Incorrectly

I want to put background color on datagridview cell if the Temperature is >=7. I'm using below code and it's working fine for temperature 7.01 to 9.99 but if the temperature is 10.01 and above, background color is not showing. Appreciate any…
phil_19
  • 105
  • 1
  • 9
1
vote
1 answer

Matching BIT to DATETIME in CASE statement

I'm attempting to create a T-SQL case statement to filter a query based on whether a field is NULL or if it contains a value. It would be simple if you could assign NULL or NOT NULL as the result of a case but that doesn't appear possible. Here's…
beardog
  • 201
  • 1
  • 2
  • 9
1
vote
0 answers

Case statement with multiple records

For a requirement I need to create a SQL which should check the data from Table A. If data exists in table a, query should return that result. If data doesn't exists on table A, query should check data in table B and return the result. I tried to…
Mohit
  • 41
  • 1
  • 1
  • 2
1
vote
2 answers

Scala sum of all elements in the leaves of a tree using pattern matching

I'm learning Scala by working the exercises from the book "Scala for the Impatient". One of the questions asks: /** * Q5: One can use lists to model trees that store values only in the leaves. For example, the list ((3, 8) 2 (5)) * describes…
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
1
vote
3 answers

SQL Server Query with "ELSE:"

I have various VB6 projects I'm maintaining with some of the queries being passed to the server having "ELSE:" with the colon used in case statements. I'm wondering can someone tell me what the **ll the colon is used for? It causes errors in…
Mike D
  • 159
  • 1
  • 1
  • 13
1
vote
1 answer

Nested Case Statement in SQL

I am getting an error stating missing right parenthesis but I cannot for the life of me figure out where in my statement. (CASE WHEN A.AUTH_STRT_DT > CD.SVC_STRT_DT THEN (CASE WHEN ABS(A.AUTH_STRT_DT - CD.SVC_STRT_DT) = 1 THEN '1 DAY BEFORE' …
tia97
  • 350
  • 4
  • 8
  • 16
1
vote
2 answers

Mutable variable and case statement in Haskell

So I'm not sure if what I want is in fact a mutable variable, but it's something likened to it. I essentially want to do this: case thing of True -> p <- func False -> p <- otherFunc return Record {program=p, otherFields=oF} Any way I can…
Gentatsu
  • 696
  • 1
  • 6
  • 26
1
vote
1 answer

VBA Variable Scope in a Case Statement

Wondering how variables work when used in a Case statement. It seems like they are declared in the first Case, regardless of whether that Case is relevant. 'The following code will throw an error Select Case team Case "Philadelphia Eagles" …
jwinigrad
  • 13
  • 3