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.

421 questions
531
votes
2 answers

What is the Python equivalent for a case/switch statement?

I'd like to know, is there a Python equivalent for the case statement such as the examples available on VB.net or C#?
John Alley
  • 5,461
  • 3
  • 11
  • 4
140
votes
5 answers

Ruby class types and case statements

What is the difference between case item.class when MyClass # do something here when Array # do something different here when String # do a third thing end and case item.class when MyClass.class # do something here when Array.class # do…
Daisy Sophia Hollman
  • 5,148
  • 6
  • 21
  • 32
83
votes
3 answers

How to use patterns in a case statement?

The man page says that case statements use "filename expansion pattern matching". I usually want to have short names for some parameters, so I go: case $1 in req|reqs|requirements) TASK="Functional Requirements";; met|meet|meetings)…
Ramiro Rela
  • 831
  • 1
  • 7
  • 3
30
votes
4 answers

T-SQL Conditional WHERE Clause

Found a couple of similar questions here on this, but couldn't figure out how to apply to my scenario. My function has a parameter called @IncludeBelow. Values are 0 or 1 (BIT). I have this query: SELECT p.* FROM Locations l INNER JOIN Posts p on…
RPM1984
  • 69,608
  • 55
  • 212
  • 331
21
votes
5 answers

Case Statements versus coded if statements

What is more efficient - handling with case statements in sql or handling the same data using if statements in code. I'm asking because my colleague has a huge query that has many case statements. I advised her to take stress off of the DB by coding…
Eric
  • 7,366
  • 17
  • 91
  • 126
20
votes
1 answer

VB.NET Stacking Select Case Statements together like in Switch C#/Java

Seems If I stack the Cases together they don't work as one. Since VB.NET Cases don't require the use of Exit Select / Return it seems to automatically put that every time a new Case is detected under it? Dim Test as Integer = 12 Select Case Test …
SSpoke
  • 5,239
  • 8
  • 66
  • 112
19
votes
1 answer

Ruby/Rails passing array to case

Here is the scenario: case code when 'www', '', nil false when 'code1', 'code2'... 'code_n' # The array STORE_CODES contains all the codes true else false end How can I use STORE_CODES directly after when instead of 'code1',…
Hable
  • 193
  • 1
  • 5
18
votes
5 answers

Why is CharInSet faster than Case statement?

I'm perplexed. At CodeRage today, Marco Cantu said that CharInSet was slow and I should try a Case statement instead. I did so in my parser and then checked with AQTime what the speedup was. I found the Case statement to be much slower. 4,894,539…
lkessler
  • 19,414
  • 31
  • 125
  • 196
17
votes
3 answers

Case statements evaluate to strings

I've caught the functional programming bug, so naturally nothing is good enough for me anymore. ;) So, in bash one could write: case $status in "foo") status="bar" ;; "baz") status="buh" ;; *) status=$status ;; esac but I'm afraid of typos,…
mbac32768
  • 11,025
  • 9
  • 31
  • 39
16
votes
2 answers

Is it possible to perform a "LIKE" statement in a SSIS Expression?

I'm using a Derived Column Task to change column data using a CASE WHEN statement. However, I need to be able to say.. SQL CODE WOULD BE: CASE WHEN Column01 LIKE '%i%' THEN '0' ELSE '1' END In SSIS Expression Language that would be: [Column01] ==…
iamtheratio
  • 559
  • 4
  • 9
  • 16
14
votes
3 answers

Pattern matching variables in a case statement in Haskell

If I compare a string literal to a string literal using the case statement, I get the expected behavior: if they are the same - it matches, if they are not - it does not. However, if I compare a string literal to a constant that is a string, I get…
Max K
  • 306
  • 2
  • 6
14
votes
3 answers

Can the SQL Case Statement fall through?

Is there a way to make a CASE statement in SQL fall through like the case statement in C#? What I don't want to do is the example below but if that’s my only option I guess I'll go with it. EXAMPLE: @NewValue = CASE WHEN @MyValue = '1'…
norlando
  • 3,483
  • 6
  • 35
  • 49
12
votes
2 answers

Mysql CASE NOT FOUND for CASE STATEMENT on a Stored Procedure

im trying to create a stored procedure that have multiples CASE STATEMENTS I have the following stored procedure: BEGIN CASE @olds WHEN 'emp' THEN CASE @news WHEN 'loc' THEN UPDATE equipos SET pe=pe-1,pg=pg+1 WHERE id=@eqloc; …
Enrique Benitez
  • 595
  • 2
  • 10
  • 27
11
votes
2 answers

Case Statements/Decode Function in Informatica

Could anyone help me with writing case statements in Informatica PowerCenter Designer? I am fairly new to Informatica, and based on my limited experience I feel case statements aren't supported. There is a decode function with similar functionality,…
11
votes
2 answers

Conditional value replacement in SQL Server

I have a table that has several columns. The value of one column is 0 or 1. I want to write a query that returns "Hello" if the value was 0, or "Bye" if it was 1. What is the appropriate way to write this query?
Tavousi
  • 12,568
  • 15
  • 47
  • 65
1
2 3
28 29