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

SQL Server 2000: select into case when in order by clause

I am trying to select rows into a temporary table with a CASE statement in the ORDER BY clause but records are not being sorted on insert. Declare @orderby varchar(10) , @direction varchar(10) set @orderby = 'col1' set @direction = 'desc' select…
user1430949
  • 849
  • 1
  • 6
  • 10
0
votes
2 answers

PHP function error Can't use function return value in write context - function returning an array. Brain slowly imploding

I am calling a function that returns an array, but I'm running into this error: Can't use function return value in write context Lots of people get this error, but I can't see what I've done wrong. This is the function: function…
Gem
  • 51
  • 1
  • 8
-1
votes
1 answer

Way to prevent case statement from excluding values?

i'm pretty new to the sql world and i've seem to run into a roadblock. Multiple conditions seems to be a regular question but I couldn't find a thread on my current problem. I'm using google data studio and have a dataset in google sheets that…
J Jo
  • 11
  • 2
-1
votes
1 answer

How to implement this CASE Statement logic on my Date variable?

I am using SQL Server 2014 and I need to implement a specific CASE STATEMENT logic in my T-SQL query. I need to create a new column (Period) in my output based on a column in the Table I am running the query against. This column will contain either…
user3115933
  • 4,303
  • 15
  • 54
  • 94
-1
votes
1 answer

MySQL Workbench - SELECT is not valid at this position with this server version when using CASE WHEN statement

When implementing a basic CASE WHEN statement in MySQL Workbench, I am getting the following error "SELECT is not valid at this position for this server version. Expecting FOR, LOCK, TABLES, VALUES, WITH, '('". Here is my SQL query: SELECT mode_dsc,…
-1
votes
1 answer

Combining three columns of text data in Google Data Studio

I have a table of data like so. Comment 1 Comment 2 Comment 3 This is a text comment null null null this text comment has a number 2 null null null this is comment 3 I want to create a calculated field such that I have a column that…
Deca
  • 11
  • 5
-1
votes
2 answers

How do you use ANY within a CASE statement?

I have a Postgres query I'm working on that passes a parameter to the query through an API call. I've modified it to run locally in SQL Developer. WHERE (string_to_array( name ,',')) = CASE WHEN 0=1 THEN (string_to_array( name ,',')) ELSE …
-1
votes
1 answer

How do I apply an IF and ELSEIF statement in Python?

I have a date field called date1 and a weekday field called weekday in a dataframe called df. I am trying to create another field that if the weekday is a certain value, add a year,otherwise add a day. It comes up with an error: ValueError: The…
-1
votes
1 answer

How I can use CASE in a different way?

Hello I am trying to build a project but the case '2' is not working. struct student { string fname;//for student first name string lname;//for student last name string id;//for Registration No number string course;//for…
-1
votes
3 answers

How to retrieve single record for a column based on SQL Server CASE statement

I am trying to write a SQL Server CASE statement to retrieve a student's category based on the following condition. If the category column from the student_category table has both values 'X' and 'Y' for a student_id value from the student table,…
-1
votes
3 answers

MIN MAX using CASE statement in SQL Server

I have a table like this. All I want in country Name, Population number and if the population in MAX then label MAX and similarly for MIN. I have written below query in SQL Server using the CASE statement. /* MIN/MAX Population amongst the…
-1
votes
3 answers

Case in Select query in sql

Below is my SQL with a case statement. select a.AssociationClubNumber, case a.AssociationClubNumber when NULL then 'NA' when '' then 'NA' else a.AssociationClubNumber end as 'AssociationClubNumber' from …
Kgn-web
  • 7,047
  • 24
  • 95
  • 161
-1
votes
1 answer

Spotfire case statement

I need to create a case statement in Spotfire please help. Case WHEN UniqueCount([Product])<5 then UniqueConcatenate([Product]) else "Multipl Products" WHEN UniqueCount([Division])<5 then UniqueConcatenate([Division]) else "Multipl Dev" END
M. Smith
  • 1
  • 5
-1
votes
1 answer

Case Statement results based on another field

Im trying to create a case statement to only show the value of AmountRequested based on the value in MortgageStatus Basically if the MortgageStatus = 4 or 5 or 6 or 7 then i want to see the value of AmountRequested else show me 0 SELECT s.ISDESC as…
RustyHamster
  • 359
  • 1
  • 5
  • 19
-1
votes
1 answer

Updates and Self Joins and Case Statements - oh my

I think I may be trying to do too much with one query, and it's been driving me batty. I have two tables, Source and Zip_Code. The Source table has a zip code field that can either be 3 or 5 digits. If it's 3 digits, I need to join to the zip_code…