Why does this case statement not work? - sql

Why does this case statement not work? My table is defined as below. I want the case to return true when M = "M" or false if it is not. Something does not seem to look right with this CASE statement but this is the simple case statement. But I thought that could be as many WHEN values as needed but in this cases there is only True or False and it does not appear that there could be anything different.
The input should be all of the M column in the row. I am expecting the output to be one row since I only have one row that has an M value in the column. I am not certain if the case statement should return false in this case.
CASE M = "M <-- is this is what the criteria is based on
I found that this is not the case however from the post below. When i meant that this did not look right I was referring two the fact that the WHEN portion of the code
WHEN true THEN something
just did not look like it normally does.
Code:
select CASE M = "M"
WHEN true THEN "PPP"
WHEN false THEN "False -- Look Again!"
ELSE "Not Found"
END
from Bat;
Image:
New Code:
select *,
CASE
WHEN M = 'M' THEN
Case
when O = 'O' then 'you found a double (MM)' <--Compiles but does not show result
End
WHEN M is null THEN 'False -- Look Again!'
END as 'What is this value'
from Bat;
That fixed the first problem but you can embed case statements N deep I would think so if i search again on O it should return a correct. But the first case returns only one record. So my second case would have to search in the record correct.

The normal way to write this is:
select (CASE WHEN M = 'M' THEN 'PPP'
WHEN M <> 'M' THEN 'False -- Look Again!'
ELSE 'Not Found'
END)
from Bat;
or:
select (CASE WHEN M is null then 'Not Found'
WHEN M = 'M' THEN 'PPP'
ELSE 'False -- Look Again!'
END)
from Bat;
If I had to guess, your version has unexpected behavior for NULL values, but it is hard to say without knowing what you expect or what it is doing.

Change it like this
select CASE
WHEN M = 'M' THEN 'PPP'
WHEN M is null THEN 'False -- Look Again!'
ELSE "Not Found"
END
from Bat;

Related

missing right parenthesis when using if

I am trying to use simple IF query in sql developer (Oracle), but i get 'missing right parenthesis' Error. I want to write all ID from my table TEST_TABLE, and write yes if ID is 1234, else 'no'. What am i doing wrong?
select id, if(id = 1234, 'yes', 'no') from TEST_TABLE t
Can't use IF there (IF is for control flow in PLSQL code, not queries), use CASE WHEN instead:
select id, CASE WHEN id = 1234 THEN 'yes' ELSE 'no' END as idIs1234 from TEST_TABLE t
The basic form of a case when is:
CASE
WHEN test THEN truevalue
[WHEN othertest THEN othertruevalue]
[ELSE falsevalue ]
END
CASE must do one or more tests that each return a single value. If no case matches (all tests are false) and there is no ELSE clause, null is returned.
There is also a form that tests a single variable for equality against various values:
CASE id WHEN 1234 THEN 'yes1' WHEN 2345 THEN 'yes2' ELSE 'no' END

Case statement in where clause using not like and is not null

I'm pulling data from an existing table using a stored procedure that has some yes or no choices that the user picks on the front end through a checkbox. I want to limit writing a bunch of different If statements for every choice they make.
This portion of my where clause works. Data is either Y or N for this column.
Where... and IsSigned = Case When #IncludeSigned = 'Y' then IsSigned else 'N' end
I would like to add to the where using is not null and not like if this is possible between the square brackets. So far I have
and SignatureType = case when #IncludeElectronic = 'Y' then Type else [NOT like electronic] end
also
and ReviewDate = Case When #HasReviewDate = 'Y' then [ReviewDate is not null] else null end
This may help you use AND/OR instead of case
where (ReviewDate is not null or #HasReviewDate = 'Y' ) And (....)
ie when #HasReviewDate = 'Y' query will return the records with ReviewDate is not null
and when #HasReviewDate != 'Y' then query will return the records with ReviewDate is null
think of it this way:-
Your first case statement has two possible results:-
IsSigned = 'Y'
IsSigned = 'N'
Your subsequent ones have problems as they don't make sense syntactically. So the second one as written returns
SignatureType = Type
SignatureType = [NOT like electronic]
and your third:
ReviewDate = [ReviewDate is not null]
ReviewDate = null end
SO the operator has to be before the case statement and apply to all of the results of the case statement.
For example
WHERE myfield not like CASE WHEN thatfield=1 THEN 'Fish' ELSE 'Chips END
would produce either
myfield not like 'Fish'
myfield not like 'Chips'
I believe you can not use in this way, apart of that, the impact that is not like can have inside your query can be high, my recommendation changes the strategy that you are using.

Condition SQL Server

This is my problem: I added a column Cause to my table. This column contains different conditions (up to here, everything is fine). But since I have a lot of lines for each product, it can have 3 conditions at the same time.
What I'm trying to do is that once it finds a condition, it does not go to the one after (and it is by this order of priority).
I do not know if I was clear, but if you want more explanation do not hesitate to ask me questions
Cause = (CASE
WHEN Four IS NOT NULL THEN 'Retards'
WHEN (MAX(DateP BETWEEN '2018-10-24' AND '2018-10-14') THEN 'stock'
WHEN Reference = 0 THEN 'respecté'
WHEN Produit = 2 THEN 'non respecté'
ELSE 'Erreur'
END)
This is an example of what I want to do:
The CASE expression stops just after the first WHEN..THEN is found. If you want to concatenate labels and check all conditions, you can use multiple CASE expression.
(case when Four IS NOT NULL THEN 'Retards' ELSE '' END +
case when (MAX(DateP) between '2018-10-24' AND '2018-10-14') THEN 'stock' ELSE '' END +
case when Reference = 0 THEN 'respecté' ELSE '' END +
case when Produit = 2 THEN 'non respecté' ELSE '' END
) AS Cause

Using SQL to return 6 check box flags into three columns

I am trying to figure out how to turn multiple check box results in differnet fileds into seperate columns.
The current case statement below only tracked the lowest score into a a single filed called 'Activities Registered For (1) – (5)'. I would like to convert them into 5 columns 'a-e' where 'a' is always filled with a result, and if two options are checked the results are in 'a' and 'b'. The form can be filled in with up to all selections checked. The else statement appears to be an error, since there are to be at least one of the five boxes checked.
I am new to SQL and I adopted this from someone else, so I am sorry for not showing my previous attempts to resolve my issue.
,CASE
WHEN [1524#1] = 'Y' THEN '1'
WHEN [1525#1] = 'Y' THEN '2'
WHEN [1526#1] = 'Y' THEN '3'
WHEN [1527#1] = 'Y' THEN '4'
WHEN [1528#1] = 'Y' THEN '5'
ELSE ' ' END AS 'Activities Registered For (1) – (5)'
You could use a PIVOT but multiple CASEs are just as effective, use about the same amount of code and is easier for beginners to decipher.
CASE WHEN [1524#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_1,
CASE WHEN [1525#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_2,
CASE WHEN [1526#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_3,
CASE WHEN [1527#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_4,
CASE WHEN [1528#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_5
I don't think the ELSE is necessarily an error. I often add an ELSE than shouldn't be used in case there is unexpected data (no Y in any field in your example).
Here's some info on PIVOTs if you want to check it out:
http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query

Dynamic where clause based on variable

I am working on a select statement in SQL and am running into issues trying to create a where clause that includes a case statement or an if else statement. I want to select records based on the value of a variable. If the variable is 'True' then only return records from the select statement where a column is null. If the variable is not 'True' then return all records regardless if that columns is null.
Any tips on how to do this?
Below is a simple example of what i am trying to do:
declare #option1 as varchar(5)
--This can be True or False so to test i just put the Set option below
set #option1 = 'True'
Select a,b,c,d...
from ...
where d = case when #option1 = 'True' then NULL End
This is the part where i do not know what to do. I only need to filter out the records if the variable is 'True' so not sure what to put in the else section of the case.
You can't test for d = NULL as your CASE statement does because that will always return false since NULL is not equal to NULL (unless you set ANSI_NULLS to 'off').
The simplest thing to do would be to change the WHERE clause to this:
WHERE #option1 = 'False' OR d IS NULL
If you prefer to use a CASE statement for some reason, you can write it like this:
WHERE 1 = CASE WHEN #option1 = 'False' THEN 1
WHEN #option1 = 'True' AND d IS NULL THEN 1
ELSE 0
END
This:
UPDATE: PinnyM has straightened me out on this. I am leaving my embarrassing logically flawed argument here for the education of the masses. The solution I propose below after "Try this" is certainly still valid, but PinnyM's solutions is by far more elegant and should be used.
WHERE #option1 = 'False' OR d IS NULL
Will always return all the results given his current select statement (assuming #Option1 is simply a flag parameter passed in).
Try this:
SELECT a, b, c, d
WHERE
-- Returns only rows where d is null (if #Option1 is True)
(#Option1 = 'True' AND d IS NULL)
OR
-- returns all the rows (if #Option1 is False)
(#Option1 = 'False')