In SQL Server 2008 is there an abbreviated way to match CASE boolean expressions, with the values you'd like to select when the expression is true?
Instead of
CASE
when item='a' then 'Apple'
when item='b' then 'Ball'
when item IN ('c','d') then 'Pet'
when item !='zz' then 'object'
else 'Bad_Item'
I would like something resembling one of the following. These are of course pseudo code, but the idea is to bring the association closer and not to keep writing more when/then pairs
Does something like this exist?
CASE
when item ;(=,'a','Apple')
;(=,'b','Ball') ;(=,'c' or 'd','Pet')
;(!='zz','object') ;'Bad item'
END
Does something like this exist?
CASE
when item ;= ('a','b','c' or 'd' : 'Apple','Ball','Pet','Object')
;!= ('zz' : 'Object')
;'Bad item'
END
Again those were pseudo code, but just wanted to know if there is something faster or simpler or with a listing of all the things to check followed by all the values to select.
There is no shorthand like you describe. You can shorten the code a little bit by using the alternate CASE syntax that #sarwar026 mentioned, but the <> line he posted doesn't work. You can't use that form with anything but equality - no inequality, no ranges using between or >=/<=, no IN(), no null checks even.
An example of combining the simple and searched CASE expressions is:
declare #item as VarChar(10) = 'c'
select case #item
when 'a' then 'Apple'
when 'b' then 'Ball'
else case
when #item in ( 'c', 'd' ) then 'Pet'
when #item != 'zz' then 'object'
else 'Bad_Item' end
end
The following works as far I know
CASE item
when 'a' then 'Apple'
when 'b' then 'Ball'
when 'c' then 'Pet'
when 'd' then 'Pet'
// the next line is not possible, so please discard the next line
*when <>'zz' then 'object' // not sure about this syntax*
else 'Bad_Item'
END
Related
I'm trying to use CASE WHEN to group multiple possible values in a SQL view. It works, but backwards and I'd like to understand why.
SELECT DISTINCT
name,
CASE
WHEN name NOT LIKE '%Value1%' THEN 'Group1'
WHEN name NOT LIKE '%Value2%' THEN 'Group2'
WHEN name NOT LIKE '%Value3%' THEN 'Group3'
ELSE name
END AS 'filtered name'
FROM sometable;
This actually gives me the output that I need:
Anything that contains Value1 is put in Group1
Anything that contains Value2 is put in Group2
Anything that contains Value3 is put in Group3
Anything else keeps it's current name
Now, I expected the query for this result to be the one I currently have without any 'NOT' before the 'LIKE' operators, and I am quite confused that this works, I am trying to understand what's happening.
I have a field "model" in my table Vehicles (which has just under 59,000 entries) which may have a value of something like:
Roadline (09-14)
I want to remove the (XX-XX) if it exists and fill in the "treated" value to a field modelname.
Any help will be greatly appreciated!
A very simple solution is to use SUBSTRING_INDEX to isolate the portion before (. You can use REGEXP to make sure the pattern matches but, unfortunately, you cannot use it to capture matches.
SELECT
model,
CASE
WHEN model REGEXP '\\([0-9]+-[0-9]+\\)$' THEN SUBSTRING_INDEX(model, '(', 1)
ELSE model
END AS modelname
FROM vehicles
Once you have made sure the data looks OK, you can update the other column like this:
UPDATE vehicles
SET modelname = CASE
WHEN model REGEXP '\\([0-9]+-[0-9]+\\)$' THEN SUBSTRING_INDEX(model, '(', 1)
ELSE model
END
I'm trying to separate one column that contains a blood type into two columns.
For example, if my value is ABNG in the existing blood type column, I want to separate this into two new columns and insert the values: AB and Neg.
Not sure if a case statement will handle this, but I tried different variations and could not find a solution.
Here is what the existing data looks like, there are 9 blood types (including the Unknown which is fine) with some bad data mixed in (' ', 0, and B).
Without having the best grasp on how to handle this I am thinking a view could be created that would split the information into new columns.
And this is what I'm hoping the end result to be:
Is this possible?
You can use CASE statement for checking conditions and wildcard characters for string filtration for the source column.
SELECT PBTYPE [Existing Blood Type],
CASE
WHEN PBTYPE LIKE 'O%' THEN 'O'
WHEN LEFT(PBTYPE,1)='A' AND SUBSTRING(PBTYPE,2,1)<>'B' THEN 'A'
WHEN LEFT(PBTYPE,2)='AB' AND SUBSTRING(PBTYPE,2,1)='B' THEN 'AB'
WHEN PBTYPE LIKE 'B%' THEN 'B'
WHEN PBTYPE = 'UNK' THEN 'UNK'
END [Blood Type],
CASE
WHEN PBTYPE LIKE '%POS' OR PBTYPE LIKE '%PS' THEN 'POS'
WHEN PBTYPE LIKE '%NEG' OR PBTYPE LIKE '%NG' THEN 'NEG'
WHEN PBTYPE LIKE '%B' THEN 'B'
WHEN PBTYPE LIKE '%UNK' THEN 'UNK'
END [Rho]
FROM YOURTABLE
WHERE PBTYPE <> '' AND PBTYPE <> '0'
Click here to view result
Given that there's a limited number of blood types (and hoping that your data is formatted well), you could do something like:
SELECT Type = CASE WHEN col LIKE 'AB%' THEN 'AB' /*gets AB*/
ELSE LEFT(col, 1) /*gets o,a,or b*/ END,
Valence = RIGHT(col, 3) /*gets pos or neg*/
I would like to use REGEXP in a CASE WHEN context like this:
SELECT
CASE title
WHEN REGEXP '^(The Matrix|Riddick|American Pie)$' THEN (
'Movie'
) ELSE (
'FOOO'
)
END
FROM `movies`
but this is not possible. I would like to match different strings here as you can see.
Regards,
Felix
This is indeed possible, with the correct syntax. REGEXP requires a left and right side operand, so use the other syntax of CASE where the full expression is placed after WHEN.
SELECT
CASE
WHEN `title` REGEXP '^(The Matrix|Riddick|American Pie)$' THEN 'Movie'
ELSE 'FOOO'
END AS column_alias
FROM `movies`
However, if you are not using any variable elements in the regular expression, this is likely to be far less efficient than doing exact matches on an indexed column. In other words, you have no need for a regular expression with the example you gave.
SELECT
CASE
WHEN `title` IN ('The Matrix', 'Riddick') THEN 'Movie'
ELSE 'FOOO'
END AS your_column_alias
FROM `movies`
I'm trying to use a IF...ELSE function in a MySQL WHERE statement without success.
I have this query:
SELECT id
FROM mytable
WHERE restrcountry NOT LIKE '%*nl*%'
AND (IF languages LIKE '%*nl*%', 1, 0) = 1;
Ok, this is my query with IF statement.
However, how can I use also "ELSE"?
Example, I would like to do something similar:
IF language match nl ---> select id field where language is nl
ELSE IF language NOT match nl ---> select id field where language is en
How can I do this in a MySQL query, please?
Thanks to all!
The syntax for IF is :
IF(test_expr, then_expr, else_expr)
so you could do something like IF(test1, result1, IF(test2, result2, else_result)) but it would not be very readable, so there's the CASE expression for that purpose.
CASE WHEN test1 THEN result1
WHEN test2 THEN result2
ELSE else_result END
If you want to condition a select column, you can use the IF in the select fields directly:
SELECT IF(match, nl_column en_column) AS lang
FROM table
Note that an expression in a where clause is either TRUE or FALSE, so writing
IF(expr, TRUE, FALSE)
is the same as
expr
use CASE instead
CASE
WHEN languages LIKE '%*nl*%' THEN 1
WHEN languages NOT LIKE '%*nl*%' THEN 0
END as languages