Hi I'm trying to put on a validation on my where clause. But this validation needs only to exist when a certain condition is met else i dont need this validation.
I tried but i am not getting my expected output. Maybe I missed something? Please help
WHERE (CASE
WHEN 1 = (SELECT DECODE (PARAMETER_VALUE, 'FALSE', 1, 2)
FROM TABLE_A
WHERE PARAMETER_NAME = 'STUDENT_STATUS')
THEN
INVALID_STAT
ELSE
' '
END) = 'Y'
What about replacing the last ' ' to 'Y'. So when the query returns a result it converts to
WHERE INVALID_STAT = 'Y'
And when the query doesn't return any result it converts to -
WHERE 'Y' = 'Y'
Thus ignoring the condition.
Related
Thank you in advance for any help!
I am having difficulty writing a where statement for an SSRS report I am writing. What I would like to do is have a parameter for 'Sites', for example, but allow the user to either enter in specific site numbers(which can include letters) or enter 'ALL' to just return all results.
What I have tried is:
((Case
WHEN (Upper(:Site) = 'ALL') THEN ('1')
WHEN ((Upper(:Site) != 'ALL')
AND (SITE_NUMBER IN Upper(:Site))) THEN '1'
ELSE '0'
END) = '1')
and
((Case
WHEN LENGTH(:Site) = 3 THEN ('1')
WHEN (LENGTH(:Site) <> 3
AND (SITE_NUMBER IN (:Site))) THEN '1'
ELSE '0'
END) = '1')
(The issue with the second one is that sometimes my value may be 3 digits)
What I have found is that these both seem to work until I enter multiple values in for :Site. If I do that I get ORA-00909: invalid number of arguments.
Normally I would just do a NVL qualifier but SSRS does not allow a parameter to both allow multiple values and allow NULL values.
I also thought about just doing a row visibility expression on my report but I need to do this for about 10 parameters so I don't think that is a feasible option.
Thanks again for any thoughts you may have!
How about a simple boolean expression?
where (upper(:Site) = 'ALL' or site_number = upper(:site))
You need to try the following WHERE clause:
CASE
WHEN UPPER(':Site') = 'ALL' THEN '1'
WHEN SITE_NUMBER IN (
SELECT
REGEXP_SUBSTR('Site', '[^,]+', 1, LEVEL)
FROM
DUAL
CONNECT BY
REGEXP_SUBSTR('Site', '[^,]+', 1, LEVEL) IS NOT NULL
) THEN '1'
ELSE '0'
END = '1'
Cheers!!
I have a certain validation on my SQL Query.
WHERE USER_ACTIVE = 'Y'
But I want this validation to appear only when my FLAG is set to TRUE.
like, when FLAG is set to true, check user_active column.
when FLAG is false, then proceed without validation.
tried below but i think its not possible
WHERE CASE WHEN FLAG = 'TRUE' then USER_ACTIVE = 'Y' else ' ' END
Any reco?
Thanks IA!
The logic is something like this:
WHERE USER_ACTIVE = 'Y' OR #FLAG <> 'TRUE'
You can also use CASE with WHERE. Just to point out, CASE statement only assigns values based on conditions. So, you need to make comparisons after the assignation is done. Try something like this:
WHERE (CASE WHEN FLAG = 'TRUE' then USER_ACTIVE else ' ' END) = 'Y'
I am trying to use the SUBSTR and NVL functions inside the case. The case is in the where clause of the select statement.
The code below gives the following error:
ORA-00905: missing keyword
AND ( CASE
WHEN SUBSTR(upper(p_open_invoice),1,1) = 'Y' THEN
NVL(P.AMOUNT_DUE_REMAINING,0) = 0
ELSE
1=1
END)
This looks like a syntax error around equal operator of NVL function.
That is not how case expressions work (in Oracle) -- there is no boolean type to return.
The simplest method is to remove the `case and express this as simple logic:
AND (SUBSTR(upper(p_open_invoice), 1, 1) <> 'Y' OR
COALESCE(P.AMOUNT_DUE_REMAINING, 0) = 0
)
If p_open_invoice can be NULL, you need to take that into account as well.
You cannot use a collation as a result for case..when statements, it's better converting the condition to
AND (( SUBSTR(upper(p_open_invoice),1,1) = 'Y' AND NVL(P.AMOUNT_DUE_REMAINING,0) = 0 )
OR SUBSTR(upper(p_open_invoice),1,1) != 'Y' )
If you're accustomed to programming in PL/SQL you may have seen that there's a BOOLEAN type in PL/SQL. However, this is not true in the Oracle database itself. The way I usually work around this is to use character expressions which return 'Y' or 'N' instead of TRUE or FALSE.
Keeping this in mind - if you really want to use a CASE expression similar to what you had originally you can use the following:
AND CASE
WHEN SUBSTR(upper(p_open_invoice),1,1) = 'Y'
THEN CASE
WHEN NVL(P.AMOUNT_DUE_REMAINING,0) = 0 THEN 'Y'
ELSE 'N'
END
ELSE 'Y'
END = 'Y'
Here the CASE expression returns either 'Y' or 'N', which is then compared with 'Y'.
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
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')