Case When Condition in Where Clause. Use filter condition if it matches the case when condition only - sql

Is it possible to use case when condition in where clause to filter select statement.
For Eg:
Select * from table_name
where source ='UHC'
and
to_char(termdate,'YYYYMM') <= '201603';
But i want second filter condition to work only if policy number is '1'. For Eg:
case when policy_number = '1' then to_char(termdate,'YYYYMM') <= '201603';
if the policy number is not 1 then only 1st where clause should work but if policy number is 1 then both the where clause should work.
i hope i made my situation clear.

You don't need case at all:
Select * from table_name
where source ='UHC'
and ((policy_number = '1' and to_char(termdate,'YYYYMM') <= '201603')
or nvl(policy_number, '0') != '1');
With case condition will be like:
where source ='UHC' and case when policy_number = '1' then to_char(termdate,'YYYYMM') else '000000' end <= '201603');
in else you need something that is always less than '201603'. Another problem here is why you're comparing numbers as varchars? Is it really what you need?

Related

Oracle SQL filtering results from a query

I'm trying to write a query to filter results from two columns of a parameterised query.
CASE WHEN REGEXP_INSTR('%param1)s', '[|(\\\[]') > 0 AND REGEXP_LIKE(column1.'%param1)s')
THEN 'Y' END REGEXP_MATCH,
CASE WHEN REGEXP_INSTR('%param1)s', '[|(\\\[]') = 0 AND column1 LIKE '%(param1)s'
THEN 'Y' END LIKE_MATCH,
This creates two columns, REGEXP_MATCH and LIKE_MATCH.
I then want to filter from both of these columns. If REGEXP_MATCH is 'Y' then use a regular expression on param1. If LIKE_MATCH is 'Y' then use a LIKE operator on param1, else use '*' and match everything.
I'm not entirely sure what the best way to do this is though.
Is this what you want?
WHERE ( REGEXP_INSTR('%param1)s', '[|(\\\[]') > 0 AND REGEXP_LIKE(column1.'%param1)s')
) OR
(REGEXP_INSTR('%param1)s', '[|(\\\[]') = 0 AND column1 LIKE '%(param1)s'
) OR
(%param1 IS NULL)

Check if the first or second condition exists

I have a little problem selecting queries, namely when I want to check the first condition with the code below
select * from VL_Faktura_Queue where FAK_KundenNr=127849 AND (FAK_BoMatNr LIKE '%verk%' AND FAK_VerrechnetBis ='0001-01-01')
, it shows me one position, but when I add a condition where I want to check if there is a FAK_KundenNr with FAK_BomatNr LIKE '% Verk%' OR FAK_BoMatNr Like 'Zus%' also throws me different values that do not fall under FAK_KundenNr = 127849, as I can easily check that it returns my values for this KundenNr, where there is 1 OR 2 condition.
this is my query:
select * from VL_Faktura_Queue where FAK_KundenNr=127849
AND (FAK_BoMatNr LIKE '%verk%' AND FAK_VerrechnetBis ='0001-01-01') --this would be the first condition
or FAK_BoMatNr like 'Zus%' --and this the second condition
This is the individual selection I should get but in one query at the end
so my question is how can i get in one query select from these two query from the picture, thanks everyone for the help
Your parentheses are not sufficient. AND has precedence over OR, so you have FAK_KundenNr = 127849 AND (<first condition)> OR FAK_BoMatNr like 'Zus%'.
SELECT *
FROM VL_Faktura_Queue
WHERE FAK_KundenNr = 127849
AND
(
(FAK_BoMatNr LIKE '%verk%' AND FAK_VerrechnetBis = '0001-01-01')
or
FAK_BoMatNr LIKE 'Zus%'
);
In your requirement, you need to combine the "AND" operator with other logical "OR" operator.
SELECT *
FROM VL_Faktura_Queue
WHERE
(
( FAK_BoMatNr LIKE '%verk%'
AND FAK_VerrechnetBis = '0001-01-01'
) -- 1st Condition
or
(FAK_BoMatNr LIKE 'Zus%') -- 2nd Condition
)
AND FAK_KundenNr = 127849;
Please check if this solution is working for you.

Case inside 'where' section that changes the condition of an AND statement

I am creating a store procedure and i am wondering how can i add a case block in an Add statement inside the where statement.That case statement checks an input parameter and depending its value it will change the condition from greater that to smaller than and of course be added to the add conditions
So a part of the query is like:
WHERE
AND BM.Example1 IS NOT NULL
AND BM.Example2 IS NOT NULL
AND ( Case When #inputParamter= 'A' THEN AND BM.Example < 0 ELSE And BM.Example> 0 )
ORDER BY 'SEG' ASC, 'CCY' ASC
So by this approach i am thinking to extract an add statement depending on the input parameter but unfortunately i keep getting syntax errors.
Is that possible?
Yepp, just use this:
AND (( #inputParamter= 'A' AND BM.Example < 0) OR ( #inputParamter<>'A' AND BM.Example> 0) )
However, be carefull with NULL, you have to put it in the logic as a third option.
here is a similar answer using case
AND ( Case When #inputParamter = 'A' AND BM.Example < 0 THEN 'Y'
When #inputParamter <> 'A' AND BM.Example > 0 THEN 'Y' ELSE 'N' END = 'Y')

CASE expression - add one more condition to the WHEN part

I have created a PL/SQL function where I have a case expression in a SQL query. This is working fine, but when I add another when condition it will not compile. Even if I use when ... and 2 > 1, this is also not compiling.
In the below code, the commented part is not working properly.
What I want is to add one more check in my when clause. Please advise.
create or replace function FUNCTION_NAME (date1 in varchar2,value1 in varchar2)
RETURN date
IS
date2 date;
BEGIN
SELECT D DATE2
INTO DATE2 FROM (SELECT CASE (SELECT TO_DATE(MAX(G.DATE3),'DD-MON-YYYY')
FROM TABLE1 G,
TABLE2 N
WHERE G.DATE3=N.DATE3)
WHEN LAST_DAY(TO_DATE(DATE1,'DD-MON-YYYY'))
/* AND MONTHS_BETWEEN (LAST_DAY(TO_DATE(SYSDATE)),
LAST_DAY(TO_DATE(TO_CHAR(DATE1),'DD-MON-YYYY'))) */
THEN LAST_DAY(TO_DATE(DATE1,'DD-MON-YYYY'))
ELSE
TO_DATE('31-DEC-99','DD-MON-YYYY')
END D
FROM DUAL);
RETURN DATE2;
END;
What you have is a case expression (not a case statement).
Case expressions are of two kinds: "simple" (case <expr> when val1 then ... when val2 then... etc.) and "searched" ( case when condition1 then ... when condition2 then ... etc.)
You wrote your case expression as a simple case expression. You can't, then, add conditions to the WHEN part. You must change the case expression to be "searched" all the way through.
case when (select ...) = last_day(...) AND <your commented condition> THEN .....
EDIT - copying part of a clarifying comment below my Answer.
Simple case expression:
case x when 1 then ....
Can also be written as searched case expression:
case when x = 1 then ....
These two are logically equivalent. However, if we want to add "AND 3 > 1" to the WHEN part, that works only in the searched form of the case expression.
There are two flavours of CASE.
Simple CASE:
select case dummy
when 'X' then 1
end as case_demo
from dual;
Searched CASE:
select case
when dummy = 'X' then 1
end as case_demo
from dual;
In your query you are mixing them like this, which won't work:
select case dummy
when 'X' and 1 = 1
then 1
end as case_demo
from dual;
If you switch to a "searched CASE", then you can add more when conditions:
select case
when dummy = 'X' and 1 = 1
then 1
end as case_demo
from dual;

Y or N in an SQL result

Is there a way to have a query return either a 'Y' or 'N' for a column if that column has a value above a certain number say '25'.
I am using DB2 on ZOS
SELECT CASE WHEN Col1 > 25 THEN 'Y' ELSE 'N' END As Value1
FROM Table1
For MySQL:
SELECT IF(column > 25, 'Y', 'N')
FROM table
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if
There is a IF block for SQL.
http://sqltutorials.blogspot.com/2007/06/sql-ifelse-statement.html
Use the SQL CASE statement
SELECT CASE WHERE columnname > 25 THEN 'Y' ELSE 'N' END FROM table;
select
case when somecolumn > 25 then 'Y' else 'N' end as somename
from sometable;
Select If(myColumn > 25, 'Y', 'N') From myTable
Apart from the IF and CASE statements another valid alternative is to create a user defined function feeding in your column value and returning Y or N.
This has the not inconsiderable advantage that if you are selecting on this criteria in more than on SQL statement and at a later date your condition changes - to over 30 say - then you have only one place to change your code. Although using a function adds complexity and overhead so is not always optimal don't underestimate the maintenance advantage of this approach. A minor plus too is you can make the name of the function something more meaningful, and hence self documenting, than an inline CASE