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
2
votes
2 answers

Apply rank using RowNumber based on multiple conditions

Looking for some suggestions here on a project that I am currently working on. I have a table that consists of Vehicle Name Number of Contracts Sale Amount Ratio I would like to add a Row Number to the table based on certain criteria. IF there…
user2533550
  • 23
  • 1
  • 4
2
votes
1 answer

Bash case statement restart on invalid input

Trying to figure out a way to return in a case statement. For example, if you run this.... while : do clear cat<<-EOF ====================== Foo Bar Doo Dah Setup ====================== (1) Foo (2) Bar (q) Quit …
d3c0y
  • 946
  • 7
  • 13
2
votes
2 answers

Count condition in CASE clause

I am trying to get a conditional values based on count of error codes and their occurrences in specific time period from SQL query, but this query that I have written returns the first satisfied condition for all records. What am I doing…
NullPointerException
  • 3,732
  • 5
  • 28
  • 62
2
votes
1 answer

CASE Statement using Dynamic query

Base Table: date category amount 1/1/2012 ABC 1000.00 2/1/2012 DEF 500.00 2/1/2012 GHI 800.00 2/10/2012 DEF 700.00 3/1/2012 ABC 1100.00 Dynamic SQL…
JVS
  • 91
  • 2
  • 7
2
votes
0 answers

Anonymous partial function in early initializer requires "premature access to class"

Why does this fail to compile: trait Item trait StringItem extends Item { def makeString: String } trait SomeOtherItem extends Item trait DummyTrait case class Marquee(items: Seq[Item]) extends { val strings: Seq[String] = items.collect { …
Ben Kovitz
  • 4,920
  • 1
  • 22
  • 50
2
votes
1 answer

How to write a select inside case statement

I have a stored procedure that contains a case statement inside a select statement. select Invoice_ID, 'Unknown' as Invoice_Status, case when Invoice_Printed is null then '' else 'Y' end as Invoice_Printed, case when Invoice_DeliveryDate is null…
user3929962
  • 517
  • 3
  • 6
  • 23
2
votes
2 answers

SQL Server Case Statement and Aggregation functions

I have the following table data: SELECT [Quote ID], [Deductible Plan], [Age ID], [Number of Members] FROM finalResult_001 1381 $750 Deductible Plan Age 65 10 1381 $750 Deductible Plan Age 85+ 10 1371 $150 Deductible Plan …
user3345212
  • 131
  • 1
  • 5
  • 15
2
votes
2 answers

SQL Server Convert in Case Statement

I have a column called compositeRate, it is a decimal(10,2) datatype. I am writing a Select statement that should return empty string if compositeRate is 0.00 however I am getting this error: Msg 8114, Level 16, State 5, Line 1 Error converting data…
user3345212
  • 131
  • 1
  • 5
  • 15
2
votes
3 answers

Verilog case statement

Could we have any syntax where case statement is scalable? Let me explain with an example: Mux If there were only 2 select lines always @(A[1:0]) begin case (A[1:0]) 2'b00 : select = 4'b1110; 2'b01 : select = 4'b1101; 2'b10 : select =…
user1495523
  • 475
  • 2
  • 7
  • 17
2
votes
3 answers

Why does a case statement NOT allow me to set variables that will be used inside a method

Im trying to use a case statement to assign a value to a variable named "query". Depending on the value of a comboBox, the value of query will change. I assigned the "query" variable inside my method and want to use it within the method only. I get…
Nate S.
  • 1,117
  • 15
  • 31
2
votes
4 answers

Haskell - Pattern matching in Case Of "Couldn't match expected type"

I have a custom datatype similar to: data Token = Number Int | Otherthings I want to be able to use the "Number" in one way and other things in another. So I can successfully create a case statement like: parse x = case x of Number y…
golmschenk
  • 11,736
  • 20
  • 78
  • 137
1
vote
4 answers

Sql condition statements

I have an Excel sheet and have this formula below. I would like to calculate the same formula with sql. In excel formula there is a nested if condition. Is it possible with sql ? I have tried with "Case .. When .. Then .. Else .." but I could not…
qods
  • 177
  • 1
  • 3
  • 12
1
vote
3 answers

case statement returns multiple rows

I want to return multiple rows in case statement. is it possible? or is there any alternate way to do it? select case when 3 = 1 then (select orderid from order_master where noOfInstallment = installmentPaid) else …
Abhi
  • 1,963
  • 7
  • 27
  • 33
1
vote
2 answers

Why does this Ruby case statement return "can't convert nil into String (TypeError)" when used with ||?

I originally had this code written as a series of if statements, but after learning more about Ruby decided a case statement might be more appropriate. However, now it seems to be broken and I'm not sure why. location = gets.chomp.downcase case…
Sean Lerner
  • 599
  • 1
  • 5
  • 14
1
vote
1 answer

pseudo IF/Case help

Running PostgreSQL 7.x (Yeah I'm upgrading) The problem: I have three to four fields that need to be set if no data is returned. Was thinking of something like this SELECT CASE WHEN default_field IS NULL THEN field_1 = 'blah', field_2 =…
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383