SQL equivalent for IIf or Switch/case constructs
Questions tagged [case-expression]
34 questions
0
votes
3 answers
How to return columns conditionally in SQL Server query?
Say I have columns is_return_foo, is_return_bar and is_return_baz.
I need to return the foo, bar, baz columns if any of the above are respectively set to true...
Is CASE WHEN the best option?
Something like:
SELECT
CASE is_return_foo WHEN true…

user432024
- 4,392
- 8
- 49
- 85
0
votes
1 answer
Conditional Date Comparison and Assignment using Case expression in Oracle SQL
I am new to the site and SQL. I need some help with a case expression.
The requirement is as follows:
I have a table T1 with two date columns - eff and disc;
A second table T2 with 2 date columns - on_date & off_date;
I am trying to build a single…

VPPG
- 25
- 1
- 8
0
votes
1 answer
How to understand the case expression?
This example is very intuitive. There are 3 cases for xs, which are empty list, list with only 1 element and list with more than 1 element.
describeList :: [a] -> String
describeList xs = "The list is " ++ case xs of [] -> "empty."
…

user8314628
- 1,952
- 2
- 22
- 46
0
votes
2 answers
Using Keyword [IN] with Case Expression in a SQL Statement
I have an query like this , but it does not work , what's wrong with IN keyword with CASE EXPRESSION ?
Select State = case c.addressId
when in('2552','2478','2526') then 'IN'
when in ('9999') then 'OUT'
else 'UNKNOWN'
end,
name, time
from…

Abdullah
- 5,445
- 10
- 41
- 48
0
votes
2 answers
Rewriting case when in sql
Hi Im trying to rewrite the following code that uses CASE WHEN. I was thinking that I can instead use decode or something else?
The code:
create table want as select
case when (Var1<20 ) then 1
when (40>Var1>=20 ) then 2
when (Var1>=40 ) then 3…

Alex T
- 3,529
- 12
- 56
- 105
0
votes
1 answer
How to write CASE statement with condition on values from Sub-query in SQLite
The Highschooler table has data in the below format:
Highschooler ( ID, name, grade )
English: There is a high school student with unique ID and a given first name in a certain grade.
The sample data in the table is:
ID NAME GRADE
1510 …

Sreedhar Danturthi
- 7,119
- 19
- 68
- 111
0
votes
2 answers
Case expression not working as expected with 'on duplicate key update' clause
Through the following query, I want to update the ConcurrentJobs column only when the value in MaxConcurrentJobs column is greater than or equal to the value of ConcurrentJobs being updated.
insert into userjobinfo (UserId,ConcurrentJobs) values…

Suhail Gupta
- 22,386
- 64
- 200
- 328
0
votes
2 answers
Switch case expression for oracle database versions
I need to query the patch status on Oracle databases. Since Oracle version 12c the view sys.REGISTRY$HISTORY was replaced by the view DBA_REGISTRY_SQLPATCH. On older versions like 11g the view dba_registry_sqlpatch does not exist. The following…

r0tt
- 379
- 3
- 20
0
votes
2 answers
Case Expression throwing exception
Consider the following code:
Select FLAGS,
CASE FLAGS
WHEN 0 THEN "10000000"
WHEN 1 THEN "01000000"
WHEN 2 THEN "00100000"
WHEN 3 THEN "00010000"
WHEN 4 THEN "00001000"
WHEN 5 THEN "00000100"
WHEN 6 THEN "00000010"
WHEN 7 THEN "00000001"
…

NewBie
- 37
- 12
0
votes
2 answers
Why is SQL Server not processing the codes correctly?
Here is my code:
Select
cname,
case
WHEN cid < 35 then 'Failed'
WHEN cid > 35 AND < 50 then 'Below Average'
WHEN cid > 50 AND < 60 then 'Average'
WHEN cid > 60 AND < 70 then 'Good'
WHEN cid > 70 AND < 85…

NandhagaM
- 11
- 3
0
votes
1 answer
Grouping within a SQL Statement?
I have a query in SQL in which I am presenting whether or not a person has payments or outstanding charges. In this query I have a case statement, but I need to group the records together to make sure payments are added together so that what I am…

DadTo2
- 69
- 1
- 7
0
votes
1 answer
Case expression does not return the "else" value in Postgres query
I'm creating a SQL query with a lot of nested queries and I'm trying to use the CASE expression but it is behaving weirdly.
This is my query at the moment:
select t.fpl_id, t.team_name,
sum(pf.points)as gwpts,
(
select sum(transfers_malus)
…

Syl
- 1,164
- 2
- 14
- 23
0
votes
2 answers
How do I solve this subquery with case expression?
***Rules: --- Do not use a join of any kind and do not use a correlated subquery. --- Do not use a comma join. --- If you use a join, you will get no points for that task. A From clause will reference only one table. --- DO not use a…

user2690891
- 1
- 2
0
votes
1 answer
MYSQL retrieve data dependent on rows returned
I am working on a mysql query that will filter out certain occurrences dependent on how many rows are returned.
I am trying to filter out any support categories when the number of rows returned are 1, however leave the support category in when the…

Charabon
- 737
- 2
- 11
- 23
0
votes
1 answer
Mysql logic - how to change the selected value based on the data?
I am trying to populate a table with phone number from a temp table. I have wrote the query with no problem. but my peoblem here is to know if the company already has a primary number or not
so I select 2 fields from my temp table called…

Mike
- 2,735
- 11
- 44
- 68