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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Previous Quarter Oracle sql

Status
Not open for further replies.

BIProf

MIS
Oct 8, 2001
9
0
0
US
I'm looking for the sql to create a filter on a date object based on the previous quarter of the sysdate. Any insight would be greatly appreciated.
 
select hiredate, to_char(hiredate,'q') from emp
Where to_char(hiredate,'q')=to_char(decode(to_number(to_char(sysdate,'q'))-1,0,4,to_number(to_char(sysdate,'q'))-1));
 
This works but it returns data for all of the years. I need to retrieve data for the current year or previous year if the current quarter is quarter 1. Any ideas???
 
Have just modified the above query a bit:
----------------------------------------------------------
select hiredate, to_char(hiredate,'q') from emp
Where to_char(hiredate,'q-yyyy') =
decode(to_number(to_char(sysdate,'q'))-1,0,'4-'||to_char(to_number(to_char(sysdate,'yyyy'))-1),
to_char(to_number(to_char(sysdate,'q'))-1)||'-'||to_char(sysdate,'yyyy'))
---------------------------OR-------------------------------
select hiredate, to_char(hiredate,'q') from emp
Where to_char(hiredate,'q') = to_char(decode(to_number(to_char(sysdate,'q'))-1,0,4,to_number(to_char(sysdate,'q'))-1))
and to_char(hiredate,'yyyy') = decode(to_number(to_char(sysdate,'q'))-1,0,to_number(to_char(sysdate,'yyyy'))-1, to_char(sysdate,'yyyy'));
-----------------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top