Questions tagged [case-when]

Use this tag only for SQL queries with case expressions. (Use switch-statement for "case" statements in Ruby and other languages.)

Use this tag for SQL queries. Use for "case" statements in Ruby and other languages.

882 questions
601
votes
11 answers

OR is not supported with CASE Statement in SQL Server

The OR operator in the WHEN clause of a CASE statement is not supported. How can I do this? CASE ebv.db_no WHEN 22978 OR 23218 OR 23219 THEN 'WECS 9500' ELSE 'WECS 9520' END as wecs_system
Werner
  • 6,125
  • 4
  • 17
  • 11
226
votes
5 answers

Can dplyr package be used for conditional mutating?

Can the mutate be used when the mutation is conditional (depending on the values of certain column values)? This example helps showing what I mean. structure(list(a = c(1, 3, 4, 6, 3, 2, 5, 1), b = c(1, 3, 4, 2, 6, 7, 2, 6), c = c(6, 3, 6, 5, 3, 6,…
rdatasculptor
  • 8,112
  • 14
  • 56
  • 81
52
votes
4 answers

How do I use T-SQL's Case/When?

I have a huge query which uses case/when often. Now I have this SQL here, which does not work. (select case when xyz.something = 1 then 'SOMETEXT' else (select case when xyz.somethingelse = 1) then 'SOMEOTHERTEXT' …
grady
  • 12,281
  • 28
  • 71
  • 110
37
votes
4 answers

How can I SELECT multiple columns within a CASE WHEN on SQL Server?

I have searched this site extensively but cannot find a solution. Here is the example of my query: SELECT ActivityID, Hours = (CASE WHEN ActivityTypeID <> 2 THEN FieldName = (Some Aggregate Sub Query), …
Joshua
  • 581
  • 1
  • 4
  • 8
36
votes
5 answers

SQL Server CASE .. WHEN .. IN statement

On SQL server 2005 I am trying to query this select statement SELECT AlarmEventTransactionTableTable.TxnID, CASE AlarmEventTransactions.DeviceID WHEN DeviceID IN( '7', '10', '62', '58', '60', '46', '48',…
Faisal
  • 533
  • 1
  • 5
  • 9
36
votes
3 answers

Execution order of WHEN clauses in a CASE statement

Given the following body of a case statement: 1 WHEN r.code= '00' then 'A1' 2 WHEN r.code ='01' AND r.source = 'PXWeb' then 'A2' < 3 WHEN r.code ='0120' then 'A3' 4 WHEN r.code ='01' …
The Ghost
  • 681
  • 1
  • 6
  • 15
30
votes
2 answers

how to put nested case-when condition in postgresql query

i want to write nested case when condition in query to store the value that will come from one case when condition and another case when condition into same new column.to get this kind of result i am writing the query as: (case when sq_name_new1…
shyarry g
  • 345
  • 1
  • 3
  • 8
28
votes
4 answers

How to ORDER BY CASE in Doctrine2 (Symfony2)

I want to run this query by using Doctrine in Symfony 2.3. But it seems like Doctrine does not understand CASE statement. Can anyone help? Thank you in advance! SELECT max(id) id, name FROM cards WHERE name like '%John%' GROUP BY name ORDER BY CASE…
Tin Nguyen
  • 285
  • 1
  • 3
  • 6
13
votes
2 answers

Can I use .include?() in a case statement? Ruby

I have started to learn Ruby. I have a small project to build a game and tried to create a function that receives user input and handles it accordingly. def Game.listener print "> " while listen = $stdin.gets.chomp.downcase case listen …
MrSlippyFist
  • 481
  • 2
  • 6
  • 19
10
votes
3 answers

Select case comparing two columns

I would like to have a query that uses the greater of two values/columns if a certain other value for the record is true. I'm trying to get a report account holdings. Unfortunately the DB usually stores the value of Cash in a column called…
Mike O.
  • 135
  • 1
  • 2
  • 7
9
votes
6 answers

Select values row-wise based on rank among dates

Let's say I have a data frame with several rows like the following: df <- data.frame(a = c(NA,20,NA), date1 = c("2016-03-01", "2016-02-01", "2016-02-01"), b = c(50,NA, NA), date2 = c("2016-02-01",…
MBB
  • 347
  • 3
  • 18
9
votes
5 answers

Oracle SQL CASE WHEN ORA-00932: inconsistent datatypes: expected CHAR got NUMBER 00932. 00000 - "inconsistent datatypes: expected %s got %s"

Getting error ORA-00932: inconsistent datatypes: expected CHAR got NUMBER 00932. 00000 - "inconsistent datatypes: expected %s got %s" When i run the following query SELECT distinct CASE when t.cancelled = 'TRUE' then '0' else t.amount END…
Matt
  • 14,906
  • 27
  • 99
  • 149
9
votes
2 answers

How to return true or false from tsql using case when

I'm trying to return true or false based on a CASE WHEN THEN tsql statement, but the only thing that ever shows up in the results panel is the column name "IsGeneric". Where am I going wrong? alter proc Storefront.proc_IsProjectGeneric @ProjectID…
9
votes
1 answer

Sum(Case when) resulting in multiple rows of the selection

I have a huge table of customer orders and I want to run one query to list orders by month for the past 13 months by 'user_id'. What I have now (below) works but instead of only listing one row per user_id it lists one row for each order the…
greenpowerade
  • 93
  • 1
  • 1
  • 3
9
votes
4 answers

can we use CASE with EXEC

I want to select a stored proc to execute based on user input. Something like - EXEC CASE @InputParam WHEN 'XML' THEN GetXMLData @ID, 'y' WHEN 'TABLE' THEN GetTableData @ID, 'y' END Can this be done with CASE or should I consider using the If…
neuDev33
  • 1,573
  • 7
  • 41
  • 54
1
2 3
58 59