CASE in WHERE expression using IN clause - sql

I need that when var1 is equal to 'Y' the query show the result of the table with the column = 'Y', but when var1 = 'N' the result should be the data with column with 'Y' and 'N'.
I need to put it in a where clause, cause I'm using oracle forms.
I tried this way but the query didn't show any result:
SELECT *
FROM table
WHERE column1 IN ((CASE WHEN var1 = 'Y' THEN q'[('Y')]'
ELSE TO_CHAR(q'[('Y','N')]')
END))
Can you help me? Thank you.

There is no need for CASE logic here, as you can fit this into a regular WHERE clause with boolean logic, wrapping each condition (var1 = 'Y', var1 <> 'Y') in a () group.
SELECT *
FROM table
WHERE
(var1 = 'Y' AND column1 = 'Y')
OR (var1 <> 'Y' AND column1 IN ('Y','N'))
Note, I used var1 <> 'Y' here to emulate your ELSE case, but if it is only two possible values Y/N you may use var1 = 'N' for clarity.
WHERE
(var1 = 'Y' AND column1 = 'Y')
OR (var1 = 'N' AND column1 IN ('Y','N'))
Actually, if Y/N are the only possible values for column1, then it could be simplified to:
WHERE
(var1 = 'Y' AND column1 = 'Y')
-- Returns all rows for column1 if Y,N are the only possible values
-- No need to explicitly filter it
OR (var1 <> 'Y')

Related

PL/SQL Case When - Map input to other column values

I've checked elsewhere on Stack but I couldn't find a similar scenario.
I want to essentially map 1 input from a PL/SQL procedure to 3 different values in MY_TABLE; otherwise I just want to filter on the input. I would like a solution similar to the below pseudocode
SELECT * FROM MY_TABLE
WHERE COLUMN 1 = 'B'
AND CASE WHEN p_input = 'F' THEN COLUMN_1 IN ('F','A','B')
ELSE COLUMN_1 = p_input;
I don't recommend using case expressions in the where. Just use simpler boolean logic operators:
WHERE COLUMN 1 = 'B' AND
( (p_input = 'F' AND COLUMN_1 IN ('F', 'A', 'B')) OR
COLUMN_1 = p_input
)
In particular, Oracle doesn't have a boolean type in SQL (there is on in PL/SQL). So a case expression cannot return a boolean value.
If you are learning how to use case..when in where clause then following is the code:
CASE WHEN p_input = 'F' AND COLUMN_1 IN ('F','A','B') THEN 1
WHEN COLUMN_1 = p_input THEN 1
END = 1
But as suggested in other answer, it is recomended to use or And it is always a good idea to use negative condition p_input <> 'F' in second condition while using OR as follows:
(
(p_input = 'F' AND COLUMN_1 IN ('F', 'A', 'B'))
OR
(p_input <> 'F' AND COLUMN_1 = p_input)
)

Column is null using Case When SQL

I'm using SQL Developer
I need to accept a user input as either Y or N.
If N is entered, I need to convert this to null. If Y is entered it should remain Y. The script then needs to search for a column with either a null value or a value of Y depending on what is entered by the user.
I have the code below with a decent idea for how to tackle it
accept WITHDRAWN char prompt 'Please enter Y or N)'
SELECT *
FROM table1
WHERE column1 = case when '&WITHDRAWN' = 'N' then null else '&WITHDRAWN' end;
As you can probably tell, it doesn't work, as for the column to be null, the '=' sign would need to be an 'is'. Likewise for the column to be 'Y' then the sign would need to be '='.
I really can't figure out a way to substitute N for the value of null and allow column1 to match up to this. Is it even possible?
I'd coalesce the column1 value so when it's Null, N is substituted and compared to the user input.
SELECT *
FROM table1
WHERE coalsece(column1,'N') = &WITHDRAWN;
Assumption being in table1 all column1's that are null are infact 'N' so the coalesce simply allows us to do a character to character comparison.
Also be aware that indexes on column1 will be ignored since we had to use a function to get the 'N' value returned.
You don't need case at all:
SELECT *
FROM Table1
WHERE
(
'&WITHDRAWN' = 'N'
AND Column1 IS NULL
)
OR Column1 = 'Y'
select * from table1
where (
(column1 = '&WITHDRAWN' and '&WITHDRAWN' != 'N')
or
(column1 is null and '&WITHDRAWN' = 'N'))
Hope this help!
SELECT * FROM table1
WHERE (
(column1 = '&&WITHDRAWN' AND '&&WITHDRAWN' = 'Y')
OR
(column1 IS NULL AND '&&WITHDRAWN' = 'N'))

Case when statement in SQL

I am using the following query. In this query I want to apply the where clause based on passed parameter. But the issue is that where clause is like 'value = if parameterVal = 'I' than NULL else NOT NULL'
I've build a query like this
SELECT * FROM MASTER
WHERE
Column1 IS (CASE WHEN :Filter = 'I' THEN 'NULL' ELSE 'NOT NULL' END)
but it's not working. Help me solve this.
UPDATE
Updating question to elaborate question more clearly.
I've one table MASTER. Now I am passing one parameter in query that is Filter (indicated by :Filter in query).
Now when the Filter parameter's value is 'I' than it should return the following result.
SELECT * FROM MASTER WHERE Column1 IS NULL
but if the passed argument is not equal to 'I' than,
SELECT * FROM MASTER WHERE Column1 IS NOT NULL
SELECT * FROM MASTER
WHERE (Filter = 'I' AND Column1 IS NULL)
OR
(Filter <> 'I' AND Column1 IS NOT NULL)
If you really insist on using a CASE the SELECT could be rewritten as:
SELECT *
FROM MASTER
WHERE CASE
WHEN COLUMN1 IS NULL AND FILTER = 'I' THEN 1
WHEN COLUMN1 IS NOT NULL AND FILTER <> 'I' THEN 1
ELSE 0
END = 1
SQLFiddle here
Frankly, though, I think that this is very difficult to interpret, and I suggest that #MAli's version is better.
Your case has assignment not equality check

In this example can UPDATE statements be combined?

I quite often have rows of code like the following:
UPDATE my_table SET name = 'x' WHERE Original = 'a'
UPDATE my_table SET name = 'y' WHERE Original = 'b'
UPDATE my_table SET name = 'z' WHERE Original = 'c'
UPDATE my_table SET name = 'k' WHERE Original = 'd'
UPDATE my_table SET name = 'm' WHERE Original = 'e'
UPDATE my_table SET name = 'n' WHERE Original = 'f'
Can I combine/shorten this code into one UpDate statement - or are they best just left as they are?
UPDATE my_table
SET name =
CASE
WHEN Original = 'a' THEN 'x'
WHEN Original = 'b' THEN 'y'
...
END
That will update EVERY row. So if there's an Original value you haven't specified, it will be set to NULL. So you might want to limit the update to just those you want to update, with a WHERE clause, like so:
WHERE Original IN ('a', 'b', ...)
OR, as an alternative, you could use an ELSE statement, which leaves the name value as is, if it doesn't have a match in the WHEN statements, like so:
CASE
WHEN Original = 'a' THEN 'x'
WHEN Original = 'b' THEN 'y'
...
ELSE name
END
You could use a case statement:
UPDATE my_table
SET name =
case Original
when 'a' then 'x'
when 'b' then 'y'
...
else name -- Preserve original
end
The else clause makes sure you're not modifying a name if it's not matched in the case.
You could use a table value constructor and a from clause, if these values aren't already in a table:
update mt set name = t.name
from
my_table mt
inner join
(values
('a','x'),
('b','y'),
('c','z'),
('d','k'),
('e','m'),
('f','n')
) t(original,name)
on
mt.Original = t.original

Sql statement for mutually exclusive events

I am trying to run an sql statement on an iSeries that will output retuls based on a type parameter I pass in.
Just say mytable has a field called field1. field1 contains Y,N and NULL values.
A type of 'Y' should return just 'Y' values.
A type of 'N' should return not 'Y' values. (ie. Null, N and any other junk in the field)
I tried this...
select *
from mytable
where field1 in case when :type = 'Y' then 'Y'
else (select field1 from mytable where field1 <> 'Y') end
However, this does not work.
I believe the logic you are looking for is this:
SELECT *
FROM myTable
WHERE (:type = 'Y' AND field1 IS NOT null AND field1 = 'Y')
OR (:type <> 'Y' AND (field1 IS null OR field1 <> 'Y'))
(keep in mind the fact that short-circuit logic is not garuanteed with SQL...)
Remember that null doesn't really compare to anything, and it's best to call out the fact that you actually want it (in the second case).