Oracle: CASE conditional STATEMENT in the WHERE Clause is not working? - sql

I have below query with CASE statement and this is trowing "missing keyword error"
Can you please help.
select *
from podConfigKey_Tab PCK
WHERE
CASE WHEN (PCK.Keyid = 'TLMAPIConfigMgr.UseDB'
and PCK.DEFAULTKEYIDVALUE = 'FALSE')
THEN PCK.Keyid = 'TLMAPIConfigMgr.UseDB'
ELSE PCK.Keyid != 'TLMAPIConfigMgr.UseDB'
END;

A case expression returns a single value, not a syntactic construct like a=b. You could, however, emulate this behavior with a series of logical operators:
SELECT *
FROM podConfigKey_Tab PCK
WHERE PCK.DEFAULTKEYIDVALUE = 'FALSE' OR
PCK.Keyid != 'TLMAPIConfigMgr.UseDB'

your query should be something more like as mentioned below (removed the else portion to make the below query work), you need to have predicate after the WHERE clause so that you can match value that is return by the CASE statment
select * from podConfigKey_Tab PCK
WHERE PCK.Keyid =
CASE WHEN (PCK.Keyid = 'TLMAPIConfigMgr.UseDB' and PCK.DEFAULTKEYIDVALUE = 'FALSE') THEN 'TLMAPIConfigMgr.UseDB'
END ;

The Oracle CASE expression (like DECODE) returns a value, but by itself it is not a predicate which can evaluate to TRUE or FALSE. You need to establish some condition such that the value returned by the CASE statement can be evaluated. For example:
with sample_data as
(select 'dog' pet, 'y' has_fur from dual union all
select 'cat', 'y' from dual union all
select 'bird', 'n' from dual)
select *
from sample_data
where (case when has_fur = 'y' then 1 else 0 end) = 1;
SQL Fiddle Example

Related

Use a case expression on resultset?

I'm trying to write a SQL query with case and the conditions of the case is dependent on whether any records are found on a sub-query.
Select
Case When (Another Select statement which may return results) 'True'
Else 'False'
End As Has_Results
From TBL_ABC
You want an EXISTS condition in a CASE expression:
Select Case
When Exists (
Another Select statement which may return results
)
Then 'True'
Else 'False'
End As Has_Results
From TBL_ABC
You can use EXISTS:
SELECT
CASE
WHEN exists(other query) THEN 'True' --Subquery has record
ELSE 'False' --Subquery has not record
END;

How to output 'is null' in a where clause in a case when statement?

The query checks the value of a parameter foo which is passed by a dropdown inside a program. If that paramater contains a certain value, an attribute should only contain null values. Can I manage that without pl/SQL?
select * from table t
where case when $foo$ = 'yes' then t.something is null end
Do you mean this logic?
select something
from table t
where ($foo$ = 'yes' and t.something is null) or ($foo != 'yes')
Just use nvl function :
select *
from mytable t
where nvl($foo$,'yes') = 'yes';

ORACLE: USE RESULT OF CASE-WHEN-STATEMENT

I have a huge query and I am wondering if it is in Oracle possible
to get the result of a case-when-statement and use it for comparison? My CASE-STATEMENT is declared in the Select-Statement and it looks like this.
SELECT........
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST;
Now I want to get the result of this case-statement and use it in the where part? Is it possible? (Sry this may be a dumb question)
If you define your CASE statement in either an inline-view or a common table expression (aka WITH clause), you can refer to it by whatever alias you give it.
For example (inline-view):
SELECT ...
FROM ( SELECT .....
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST
FROM...
) v
WHERE v.test = 'TEST2';
As a common table expression, it would be:
WITH cte AS ( SELECT........
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST
FROM ... )
SELECT ...
FROM cte
WHERE test = 'TEST2';
You can use a case statement in the where clause, for eg.:
select * from table
where table.field = (CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END)
This will compare the value returned from the case statement with the table field.

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

Case when using IN clause

I need a query to return a certain result for a certain column depending on what value the column that is being run the select statement against has.
if the column is one of the following : I, D, U then I want to return Y
if the column is one of the following : N, E, D then I want to return N
else : I want to return NULL
I wrote the following statement but it doesn't work.
SELECT HIERARCHY_TYPE,
NODE_ID,
NODE_TYPE,
NODE_NAME,
NODE_LEVEL,
PREFERRED_ALIAS,
PARENT_NODE_ID,
CASE ACTIVE_INDICATOR
WHEN ('I' or 'U' or 'Y') THEN 'Y'
WHEN ('D' or 'E' or 'N') THEN 'N'
ELSE NULL
END
FROM MV_HIERARCHY MV;
Is there a way to rewrite it without using multiple OR clauses for each possible value?
I'd use the IN operator:
SELECT HIERARCHY_TYPE,
NODE_ID,
NODE_TYPE,
NODE_NAME,
NODE_LEVEL,
PREFERRED_ALIAS,
PARENT_NODE_ID,
CASE
WHEN ACTIVE_INDICATOR IN ('I','U','Y') THEN 'Y'
WHEN ACTIVE_INDICATOR IN ('D','E','N') THEN 'N'
ELSE NULL
END AS ACTIVE_INDICATOR
FROM MV_HIERARCHY MV;
CASE
WHEN ACTIVE_INDICATOR IN ('I','U','Y') THEN 'Y'
WHEN ACTIVE_INDICATOR IN ('D', 'E', 'N') THEN 'N'
ELSE NULL -- useless, but for readbility
END as ACTIVE_INDICATOR
You've got to repeat ACTIVE_INDICATOR, cause I don't think (may be wrong) you can use the syntax
CASE <field>
WHEN IN()
but you can use
CASE
WHEN <field> IN()
decode(mod(nullif(instr('IDUEYN',active_indicator),0),2),0,'N',1,'Y')