Here is a select query's where clause with a CASE condition statement. I have written this for satisfying the following requirement: "In case of any weekday, the result would contain the date of the next day and in case of Friday the result would include the dates of Saturday, Sunday and Monday"
where DTTM IN (case
when to_char(sysdate,'day') <> 'friday ' then to_date(sysdate+1,'DD/MON/YY')
else (select to_date(sysdate+1,'DD/MM/YYYY') + level - 1 ddate
from dual
connect by level <= to_date(sysdate+4,'DD/MM/YYYY') - to_date(sysdate+1,'DD/MM/YYYY')
)
end)
I have used IN too, but I am getting an error
ORA-01427: single-row subquery returns more than one row
- 00000 - "single-row subquery returns more than one row"
Please help me resolve this error.
Here is the complete query for your reference:
select * from orders
where DTTM IN (case
when to_char(sysdate,'day') <> 'friday ' then to_date(sysdate+1,'DD/MON/YY')
else (select to_date(sysdate+1,'DD/MM/YYYY') + level - 1 ddate
from dual
connect by level <= to_date(sysdate+4,'DD/MM/YYYY') - to_date(sysdate+1,'DD/MM/YYYY')
)
end)
order by DTTM;
CASE
statement can only return a scalar (a single value). The solution is to change the subquery to return a single row. Can you include the full query so that we see the entire logic? – Tim Biegeleisen