Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case statement in where clause - possible?

Status
Not open for further replies.

DRH192

Programmer
Apr 25, 2005
96
GB
Hello All,

I am writing a business objects report in freehand SQL that connects to an Oracle data warehouse.

I need the report to refresh every weekday. On Tuesday I need it to include data for fri sat and sun. On Mon Wed Thur and Fri I need the report to look for data from 2 days ago.

I can use sysdate-2 for the Mon Wed Thur and Fri scenarios. But how do I put an If or Case clause into the SQL to decide if the day is a Tuesday?

I'm pretty much a begginner so please explain any script!

Many Thanks

Duncan
 
Yes, CASE can be used in WHERE conditions e.g.

Code:
select * from (select trunc(sysdate) as sample_date from dual)
where sample_date = (case when to_char(sysdate, 'DY') = 'WED' then trunc(sysdate) else trunc(sysdate)-2 end)
 
...And to add to Dagon's correct observation, an Oracle CASE statement can appear anywhere in an SQL statement that any data expression can appear (since, in the final analysis, a CASE statement evaluates to a simple data expression).

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
 
Quick (minor) point. This is a CASE expression a CASE statement is slightly different construct (which is NOT available to SQL, only PL/SQL)
 
Good catch, Jim...I shooda thunka that myself. [smile]

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
 
It's a wee bit picky, but might be significant when searching for help on it (just the sort of thing that can be really frustrating to a newbie), so I thought I'd mention it :)
 
Thanks for all the helpful advice people, I was surprised to see this many replies in one day! I am about to have another crack at my report now, probably be back with some more questions.

cheers
 
The issue is that to_char(sysdate, 'DY') = 'WED' statement is not NLS-agnostic, be careful if you work for an international customer.

Regards, Dima
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top