Oracle: Case in Where clause - sql

How to achieve this in Oracle query. I am getting missing expression error in IN clause
WHERE TBL_DTL_HOST.HOST_METHOD = 'CCtxnPostRq'
AND TRUNC(TBL_DTL_FEATURE.START_DATETIME) BETWEEN TO_DATE (i_startdate, 'DD/MM/YYYY') AND TO_DATE (i_enddate,'DD/MM/YYYY')
AND (SELECTED_DNIS IS NULL OR TBL_DTL_FEATURE.DNIS = SELECTED_DNIS)
AND (TEMP_CUSTOMER_ID IS NULL OR TBL_DTL_FEATURE.CUSTOMER_ID = TEMP_CUSTOMER_ID)
AND
(CASE WHEN i_Feature='All'
THEN (TBL_DTL_HOST.FEATURE_ID IN ('F020','F021'))
ELSE (TBL_DTL_HOST.FEATURE_ID IN ('F020'));
END)
am i missing anything..? Any help would be appreciated..Thanks

Oracle doesn't treat Boolean expressions like other expressions — it handles them as syntax rather than as a type — so CASE expressions can't evaluate to Booleans.
In your case, I think the clearest code is if you just rewrite it a bit:
AND ( TBL_DTL_HOST.FEATURE_ID = 'F020'
OR (i_Feature = 'All' AND TBL_DTL_HOST.FEATURE_ID = 'F021')
)

the last part of the clause should be like this to evaluate correctly
(CASE WHEN i_Feature='All' AND (TBL_DTL_HOST.FEATURE_ID IN ('F020','F021')) THEN 1
WHEN i_Feature<>'All' AND (TBL_DTL_HOST.FEATURE_ID IN ('F020')) THEN 1
ELSE 0
END) = 1

Related

Converting multiple IIfs when converting from MS Access to SQL Server

I am new to SQL Server case statements. How would I convert when I have multiple IIFs?
IIF((P.GM_CD=6150 Or P.GM_CD>=12100),
IIF(GameXrefVar.DCLBGame = null, P.GM_VAR.GameXrefVar.DCLBGame,P.GM_VAR.GameXrefVar.DCLBGame) AS GameID,
The standard case expression is what you should just always use. Presumably you intend:
(CASE WHEN (P.GM_CD = 6150 OR P.GM_CD >= 12100) AND GameXrefVar.DCLBGame IS null
THEN P.GM_VAR.GameXrefVar.DCLBGame
WHEN (P.GM_CD = 6150 Or P.GM_CD >= 12100) THEN P.GM_VAR.GameXrefVar.DCLBGame
END) AS GameID,
= NULL never evaluates to true, so it doesn't do anything particularly useful.
There is something wrong.
Parenthesis are not balanced
The condition value is the same in both the case.
P.GM_VAR.GameXrefVar.DCLBGame
P.GM_VAR.GameXrefVar.DCLBGame
CASE WHEN (P.GM_CD = 6150 OR P.GM_CD >= 12100) AND GameXrefVar.DCLBGame IS NULL
THEN P.GM_VAR.GameXrefVar.DCLBGame END AS GameID

CASE WHEN SQL Syntax Error

I am attempting to us the CASE statement for the first time and I cannot understand why I am getting a syntax error on the last WHEN and ELSE. I am trying to extract a substring if a value starts with a specific set of characters. My Code is below:
SELECT
CASE
WHEN LEFT ([RCode],2) = 'BB' THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT ([RCode],4) = 'APT-' THEN SUBSTRING([RCode],5,LEN([RCode])
WHEN LEFT ([RCode],4) = 'PS-' THEN SUBSTRING([RCode],4,LEN([RCode])
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]
Any guidance would be appreciated.
This won't (necessarily) fix your syntax problem, but I would recommend that you use like for the comparisons:
SELECT (CASE WHEN RCode LIKE 'BB%' THEN SUBSTRING([RCode], 3, LEN([RCode]))
WHEN RCode LIKE 'APT-%' THEN SUBSTRING([RCode], 5, LEN([RCode]))
WHEN RCode LIKE 'PS-%' THEN SUBSTRING([RCode], 4, LEN([RCode]))
ELSE [RCode]
END)
FROM [Xperdyte].[dbo].[tJCLines];
Then you don't have to count characters -- and the third condition will match.
Missed off two right parenthesis.
SELECT CASE WHEN LEFT([RCode],2) = 'BB'
THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT([RCode],4) = 'APT-'
THEN SUBSTRING([RCode],5,LEN([RCode]))
WHEN LEFT([RCode],4) = 'PS-'
THEN SUBSTRING([RCode],4,LEN([RCode]))
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]

how to prevent converting the text into boolean by the use of when statement in postgresql?

select fti.pa_serial_,fti.homeownerm_name,fti.ward_,fti.villagetole,fti.status,
ftrq.date_reporting, ftrq.name_of_recorder_reporting,
case
when fti.status='terminate' then ftrq.is_the_site_cleared ='1' end as is_the_site_cleared from fti join ftrq on ftrq.fulcrum_parent_id = fti.fulcrum_id
Here, is_the_site_cleared is text type of column which is converted into boolean by the when statement written and hence does not print as 1 and takes as true. I explicitly used print '1'. But this also did not work. My aim is to display '1' in the column 'is_the_site_cleared' when the value of fti.status='terminate'. Please help!!!
How about using integers rather than booleans?
select fti.pa_serial_, fti.homeownerm_name, fti.ward_,
fti.villagetole, fti.status, ftrq.date_reporting,
ftrq.name_of_recorder_reporting,
(case when fti.status = 'terminate' -- and ftrq.is_the_site_cleared = '1'
then 1 else 0
end) as is_the_site_cleared
from fti join
ftrq
on ftrq.fulcrum_parent_id = fti.fulcrum_id ;
From the description, I cannot tell if you want to include the condition ftrq.is_the_site_cleared = '1' in the when condition. But the idea is to have the then and else return numbers if that is what you want to see.

Case statement don't work correctly

SQL SERVER 2012
In my case server conflict with '='.
why it is not working?
here is my code:
SELECT
TM.CODE_3 as Account,
TM.NAME_3,
TM.CODE_5,
case TM.Line
when TM.CODE_5 = '9491'--and RS.SUB1Sel = ('05.11 Penalties and fines')
then 'R0820-5'
else TABLE_MAIN_new.Line
end
FROM TABLE_Main_new as TM
left join Danone_Main as DM
on TM.CODE_3 = DM.CODE_3
left join Rep_Struct_2012_N as RS
on TM.CODE_3 = RS.CODE_3
Incorrect syntax near '='.
Try this
case
when TM.CODE_5 = '9491'--and RS.SUB1Sel = ('05.11 Penalties and fines')
then 'R0820-5'
else TABLE_MAIN_new.Line
End
This should work:
CASE TM.CODE_5
WHEN '9491'--and RS.SUB1Sel = ('05.11 Penalties and fines')
THEN 'R0820-5'
ELSE TABLE_MAIN_new.Line
END
When you use CASE expression, the WHEN clause should only be a value (true/false depending on your CASE expression). If no CASE expression is used then the WHEN should be a boolean expression. More info HERE.

Using a CASE statement in HQL select

Is there any way to do the following in HQL:
SELECT
case when flag = true then SUM(col1) else SUM(col2)
FROM
myTable
I guess you can (3.6, 4.3) [inline edit] ...for where-clauses:
"Simple" case, case ... when ... then ... else ... end, and "searched" case, case when ... then ... else ... end
Apparently the ability to do this was added in 3.0.4, with the limitation that you cannot use sub-selects in the else clause.
See Hibernate-Forum: https://forum.hibernate.org/viewtopic.php?t=942197
Answer from Team (Gavin):
case is supported in the where clause, but not in the select clause in HB3.
And seen in JIRA with State "Unresolved".
Below you can find a working query (hibernate on postgresql) that uses 2 case statements to replace a boolean value with the corresponding textual representation.
SELECT
CASE ps.open WHEN true THEN 'OPEN'
else 'CLOSED' END,
CASE ps.full WHEN true THEN 'FULL'
else 'FREE' END,
ps.availableCapacity
FROM ParkingState as ps
I facing the same problem in HQL then I solved the following query is
select CONCAT(event.address1,', ', CASE WHEN event.address2 IS NULL THEN '' ELSE concat(event.address2,', ') END, event.city from EventDetail event where event.startDate>=:startDate and event.endDate<=:endDate;
We use hibernate HQL query extensively and I think finally there is a hackish way of doing such a thing :
Assuming we originally had a query of
i2.element.id = :someId
Then decided to expand this to be something like this:
((i.element.id = :someId and i2.element.id=:someId) or (i2.element.id = :someId))
But there was an issue where we want it to only lookup for this based on classType so a case statement:
(case when type(i)=Item then
((i.element.id = :someId and i2.element.id=:someId) or (i2.element.id = :someId))
else
i.element.id = :someId
end)
Above will not work you could make an easy version of above work by doing:
(case when type(i)=Item then
i2.element.id
else
i.element.id
end)=:elementId
But this does not actually do what we need it to do, we want it to do exact above query, so knowing you can assign a variable at the end of a case statement in there where bit of HQL:
(
(
(case when
type(r)=Item then
i.element.id
else
i.element.id end) = :elementId
and
(case when
type(r)=Item then
i2.element.id
else
i.element.id end) = :elementId
)
or
(case when
type(r)=Item then
i2.element.id
else
i.element.id end) = :elementId
)
I have managed to make the query now work based on case statement, sure it is a lot more long winded but actually does the same as the first instance
This is an example using a string comparison in the condition:
SELECT CASE f.type WHEN 'REMOVE'
THEN f.previousLocation
ELSE f.currentLocation
END
FROM FileOperation f