Questions tagged [case]
In SQL, the CASE expression evaluates a list of conditions, returning the result for the first condition which evaluates to true. For programming languages' CASE questions, use the [switch-statement] tag instead.
9,708
questions
1792
votes
30
answers
4.6m
views
How do I perform an IF...THEN in an SQL SELECT?
How do I perform an IF...THEN in an SQL SELECT statement?
For example:
SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product
862
votes
2
answers
2.4m
views
What is the Python equivalent for a case/switch statement? [duplicate]
Is there a Python equivalent for the switch statement?
607
votes
11
answers
3.1m
views
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
281
votes
7
answers
370k
views
PostgreSQL Crosstab Query
How do I create crosstab queries in PostgreSQL? For example I have the following table:
Section Status Count
A Active 1
A Inactive 2
B Active 4
B ...
263
votes
12
answers
330k
views
How to use a switch case 'or' in PHP
Is there a way of using an 'OR' operator or equivalent in a PHP switch?
For example, something like this:
switch ($value) {
case 1 || 2:
echo 'the value is either 1 or 2';
break;...
232
votes
5
answers
369k
views
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 = ...
210
votes
4
answers
411k
views
SQL Server: converting UniqueIdentifier to string in a case statement
We have a log table that has a message column that sometimes has an exception stack trace. I have some criteria that determines if the message has this. We do not want to show these messages to the ...
200
votes
2
answers
486k
views
PostgreSQL CASE ... END with multiple conditions
Here is an extract of my table:
gid | datepose | pvc
---------+----------------+------------
1 | 1961 | 01
2 | 1949 |
3 | 1990 | 02
1 ...
197
votes
11
answers
777k
views
Can I use CASE statement in a JOIN condition?
The following image is a part of Microsoft SQL Server 2008 R2 System Views. From the image we can see that the relationship between sys.partitions and sys.allocation_units depends on the value of sys....
195
votes
10
answers
346k
views
Using Case/Switch and GetType to determine the object [duplicate]
Possible Duplicate:
C# - Is there a better alternative than this to ‘switch on type’?
If you want to switch on a type of object, what is the best way to do this?
Code snippet
private int GetNodeType(...
179
votes
14
answers
777k
views
SQL Switch/Case in 'where' clause
I tried searching around, but I couldn't find anything that would help me out.
I'm trying to do this in SQL:
declare @locationType varchar(50);
declare @locationID int;
SELECT column1, column2
FROM ...
172
votes
5
answers
62k
views
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....
163
votes
3
answers
971k
views
Case in Select Statement
I have an SQL statement that has a CASE from SELECT and I just can't get it right. Can you guys show me an example of CASE where the cases are the conditions and the results are from the cases. For ...
155
votes
8
answers
60k
views
How can I have lowercase routes in ASP.NET MVC?
How can I have lowercase, plus underscore if possible, routes in ASP.NET MVC? So that I would have /dinners/details/2 call DinnersController.Details(2) and, if possible, /dinners/more_details/2 call ...
145
votes
14
answers
312k
views
JavaScript: using a condition in switch case
How can I use a condition inside a switch statement for JavaScript?
In the example below, a case should match when the variable liCount is <= 5 and > 0; however, my code does not work:
switch (...
143
votes
6
answers
76k
views
What are the different kinds of cases?
I'm interested in the different kinds of identifier cases, and what people call them. Do you know of any additions to this list, or other alternative names?
myIdentifier : Camel case (e.g. in java ...
142
votes
14
answers
1.3m
views
"CASE" statement within "WHERE" clause in SQL Server 2008
I am working with a query which contains "CASE" statement within "WHERE" clause. But SQL Server 2008 is giving some errors while executing it. Can anyone please help me with the correct query? Here is ...
135
votes
6
answers
386k
views
T-SQL: Using a CASE in an UPDATE statement to update certain columns depending on a condition
I am wondering if this is possible at all. I want to update column x if a condition is true, otherwise column y would be updated
UPDATE table SET
(CASE (CONDITION) WHEN TRUE THEN columnx
...
129
votes
4
answers
92k
views
Guards vs. if-then-else vs. cases in Haskell
I have three functions that find the nth element of a list:
nthElement :: [a] -> Int -> Maybe a
nthElement [] a = Nothing
nthElement (x:xs) a | a <= 0 = Nothing
| a == 1 ...
114
votes
3
answers
60k
views
How do I test for an empty string in a Bash case statement?
I have a Bash script that performs actions based on the value of a variable. The general syntax of the case statement is:
case ${command} in
start) do_start ;;
stop) do_stop ;;
config) ...
110
votes
2
answers
152k
views
SQL Server IIF vs CASE
I recently came to know about the availability of IIF function in SQL Server 2012. I always use nested CASE in my queries. I want to know the exact purpose of the IIF statement and when should we ...
108
votes
4
answers
146k
views
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="...
107
votes
3
answers
435k
views
MySQL select statement with CASE or IF ELSEIF? Not sure how to get the result
I have a two tables. One has manufacturer information and includes the regions where they can sell. The other has their products for sale. We have to limit visibility of the product based on the ...
106
votes
17
answers
216k
views
Case Statement Equivalent in R
I have a variable in a dataframe where one of the fields typically has 7-8 values. I want to collpase them 3 or 4 new categories within a new variable within the dataframe. What is the best approach?...
106
votes
16
answers
84k
views
Why do we need break after case statements?
Why doesn't the compiler automatically put break statements after each code block in the switch? Is it for historical reasons? When would you want multiple code blocks to execute?
84
votes
6
answers
187k
views
How do I use properly CASE..WHEN in MySQL
Here is a demo query, notice it is very simple, Fetches only where base_price is 0,
And still, it chooses the condition 3:
SELECT
CASE course_enrollment_settings.base_price
WHEN ...
82
votes
5
answers
157k
views
Conditional JOIN Statement SQL Server
Is it possible to do the following:
IF [a] = 1234 THEN JOIN ON TableA
ELSE JOIN ON TableB
If so, what is the correct syntax?
76
votes
11
answers
178k
views
Regarding Java switch statements - using return and omitting breaks in each case
Given this method, does this represent some egregious stylistic or semantic faux pas:
private double translateSlider(int sliderVal) {
switch (sliderVal) {
case 0:
return 1.0;
...
69
votes
9
answers
268k
views
Can I use a case/switch statement with two variables?
I am a newbie when it comes to JavaScript and it was my understanding that using one SWITCH/CASE statements is faster than a whole bunch of IF statements.
However, I want to use a SWITCH/CASE ...
69
votes
3
answers
101k
views
How does MySQL CASE work?
I know that SQL's CASE syntax is as follows:
CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE
However, I don'...
67
votes
6
answers
204k
views
Case statement in MySQL
I have a database table called 'tbl_transaction' with the following definition:
id INT(11) Primary Key
action_type ENUM('Expense', 'Income')
action_heading VARCHAR (255)
action_amount FLOAT
I would ...
64
votes
7
answers
111k
views
Regular expressions in a Bash case statement
I am using following script, which uses case statement to find the server.
#!/bin/bash
SERVER=$1;
echo $SERVER | egrep "ws-[0-9]+\.host\.com";
case $SERVER in
ws-[0-9]+\.host\.com) echo "Web ...
64
votes
5
answers
429k
views
GROUP BY + CASE statement
I have a working query that is grouping data by hardware model and a result, but the problem is there are many "results". I have tried to reduce that down to "if result = 0 then keep as 0, else set ...
63
votes
5
answers
203k
views
SQL changing a value to upper or lower case
How do you make a field in a sql select statement all upper or lower case?
Example:
select firstname from Person
How do I make firstname always return upper case and likewise always return lower ...
62
votes
8
answers
105k
views
Expression inside switch case statement
I'm trying to create a switch statement but I can't seem to be able to use an expression that gets evaluated (rather than a set string/integer). I can easily do this with if statements but case should ...
58
votes
6
answers
93k
views
PHP switch case more than one value in the case
I have a variable that holds the values 'Weekly', 'Monthly', 'Quarterly', and 'Annual', and I have another variable that holds the values from 1 to 10.
switch ($var2) {
case 1:
$var3 = '...
57
votes
4
answers
81k
views
MySQL Case in Select Statement with LIKE operator
Is it possible to combine the CASE statement and the LIKE operator in a MySQL SELECT statement?
For Example, I am trying to query a database that stores data in a single column in either one of two ...
57
votes
4
answers
22k
views
What is the reason for the weird syntax of the "case" statement in a bash/zsh script?
Looking from a programmer's point of view then shell script is just another programming language, where one has to learn and conform to the rules of the language. However, I have to admit that this ...
56
votes
3
answers
204k
views
What is the equivalent of Select Case in Access SQL?
I have a query which includes fields named openingbalance and commissions. I would like to compute values for commissions based on openingbalance, similar to this Select Case block in Access VBA:
...
54
votes
2
answers
118k
views
CASE statement in SQLite query
Why this query doesn't work? :( I tried to replace nested IF statement "...SET lkey = IF(lkey >= 11, lkey - 5, IF(lkey > 5, lkey + 2,lkey))"
UPDATE pages
SET lkey = CASE lkey WHEN lkey >= 11 THEN
...
52
votes
4
answers
135k
views
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....
47
votes
8
answers
90k
views
Convert string to Pascal Case (aka UpperCamelCase) in Javascript
Id like to know how I can covert a string into a pascal case string in javascript (& most probally regex).
Conversion Examples:
double-barrel = Double-Barrel
DOUBLE-BARREL = Double-Barrel
DoUbLE-...
47
votes
7
answers
131k
views
C# Switch-case string starting with
Is there any way to make a case condition in a switch statement where you say if a string begins with something?
ex
Switch (mystring)
{
case("abc")://String begins with abc (abcd or abc1 or abcz ...
47
votes
1
answer
40k
views
Case Expression vs Case Statement
What is the difference between a Case Expression and a Case Statement in MySQL? When can they be used, and what are the benefits of using one over the other?
Case Statement syntax:
CASE
WHEN ...
45
votes
3
answers
176k
views
TSQL CASE with if comparison in SELECT statement
I would like to use CASE statement in SELECT.
I select from user table, and (as one attribute) I also use nested SQL:
SELECT
registrationDate,
(SELECT COUNT(*) FROM Articles WHERE userId = ...
44
votes
4
answers
45k
views
Regex to change to sentence case
I'm using Notepad++ to do some text replacement in a 5453-row language file. The format of the file's rows is:
variable.name = Variable Value Over Here, that''s for sure, Really
Double apostrophe is ...
44
votes
4
answers
129k
views
Laravel Eloquent Select CASE?
Is there anyone with experience in PHP & Laravel Eloquent who can help me resolve this statement? I'm trying to inject a CASE... WHEN.. END... inside a raw() method. It seemed like it was ...
43
votes
5
answers
66k
views
Select Case on an object's type in VB.NET
I'm not sure if this valid C#, but hopefully you get the idea. :)
switch (msg.GetType()) {
case ClassA:
// blah
case ClassB:
// blah 2
case ClassC:
// blah 3
}
...
43
votes
5
answers
259k
views
SELECT query with CASE condition and SUM()
I'm currently using these sql statements. My table has the field CPaymentType which contains "Cash" or "Check". I can sum up the amount of payments by executing 2 SQL statements as shown below. In ...