blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

Jim 2.0

PL/SQL Calendar table

In addition to the T-SQL calendar table, which I have posted seperately, I went the whole hog and figured out the equivalent for Oracle. The only value I couldn't work out was the millisecond, which is not present on the standard date type in Oracle, but which can be retrieved and I am looking into this when I have time. This query uses the code to generate a list of dates in Oracle which was my first ever blog post on this site, yet somhow I'm still blogging about dates! Once again, I hope that it is self-explanatory, but feel free to leave a comment if you would like clarification.

select   to_date('&date','YYYYMMDD')-1 + level as DateRange

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'ddd') "DAY OF YEAR"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'day') "DAY"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'dy') "SHORT DAY"

,        to_char(to_date('&date','YYYYMMDD')-1 + level-1, 'd') "DAY OF WEEK"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'dd') "DAY OF MONTH"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'ww') "WEEK"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'mm') "MONTH"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'month') "LONG MONTH"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'mon') "SHORT MONTH"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'q') "QUARTER"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'yyyy') "YEAR"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'hh') "HOUR"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'mi') "MIN"

,        to_char(to_date('&date','YYYYMMDD')-1 + level, 'ss') "SEC"

from     dual

where    (to_date('&date','YYYYMMDD')-1+level) <= trunc(sysdate)

connect  by level<=1000

;

James

Published 24 April 2007 16:00 by James.Pipe

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

r said:

great job!

August 22, 2007 18:15
 

Stu Weissman said:

I appreciate our sharing this, it saved me a great deal of time coming up with the algorithm myself!

Stu-

September 19, 2007 13:46
 

dagwood said:

How would add in fiscal period (ie month, quarter or year) to this query.

Say the fiscal year starts in April.

September 25, 2007 18:12
 

James.Pipe said:

Thanks for the comments.

In answer to the question about fiscal period, the easiest and lowest tech way to do what you're asking would be to 'tweak' the date you're passing into the SQL so that it handles it according to your wishes. So for example if you want you year to start at April but the function assumes the year to start at January then you can shift all your dates 3 months back. The SQL below shows this (run it for an example)

select to_date('20070401','YYYYMMDD')-1 + level as DateRange

, to_char(to_date('20070401','YYYYMMDD')-1 + level, 'mm') "MONTH"

, to_char(add_months(to_date('20070401','YYYYMMDD'),-3)-1 + level, 'mm') "FISCAL MONTH"

, to_char(to_date('20070401','YYYYMMDD')-1 + level, 'month') "LONG MONTH"

, to_char(to_date('20070401','YYYYMMDD')-1 + level, 'mon') "SHORT MONTH"

, to_char(to_date('20070401','YYYYMMDD')-1 + level, 'q') "QUARTER"

, to_char(add_months(to_date('20070401','YYYYMMDD'),-3)-1 + level, 'q') "FISCAL QUARTER"

, to_char(to_date('20070401','YYYYMMDD')-1 + level, 'yyyy') "YEAR"

, to_char(add_months(to_date('20070401','YYYYMMDD'),-3)-1 + level, 'yyyy') "FISCAL YEAR"

from dual

where (to_date('20070401','YYYYMMDD')-1+level) <= trunc(sysdate)

connect by level<=1000

;

Change the date to '20070101' to see further how this works.

This will get you what you want simply by tricking the server into thinking your at a different point in the year, however you could also have the server configured so that the year starts at April 1st or whatever date applies to your business needs. This of course depends on who else is using that server, and apart from knowing that you can do it I'm afraid I can't help you with this specifically as it's outside my level of experience. I did find this link that may be able to help with this more sophisticated approach http://oraclesvca2.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_sched.htm

Hope this helps and thanks again for the comments, I appreciate the feedback.

James

September 26, 2007 14:10

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems