[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Date falls within Fiscal Year Query

Posted on 2006-07-11
15
Medium Priority
?
798 Views
Last Modified: 2008-01-09
i have a date field  (DATE datatype in Oracle 9i) in a table along with other fields. I need to have an sql query which filters out rows that are only in the current fiscal year.

The sql query reads like this:

select other_field from table where date_field falls within current fiscal year
0
Comment
Question by:lcor
  • 5
  • 3
  • 3
  • +3
15 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 17083856
No problem.  Please define 'fiscal year'
0
 

Author Comment

by:lcor
ID: 17083861
The solution doesn't have to be portable so it can be Oracle specific
0
 

Author Comment

by:lcor
ID: 17083866
fiscal year is Oct - Sept
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 17083869
Jim,

Ton of FY questions this week.  Must be lots of companies with July-June FY closing the books :)

Regards,

Patrick
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 17083892
Yep...

The Quick & Dirty way would be to embed the dates in your query...

select other_field from table where date_field BETWEEN '10/1/2005' AND '9/30/2006'

The better way (which most developers use) is to have a table of days (tbl_days, tbl_time, or tbl_months for just month if that floats your boat) where you have a column that determines fiscal year.  Then you can use queries that go like this...

select other_field from table
INNER JOIN tbl_time ON table.datefield = time.datefield
where tbl_time.FY = '2005'
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 17083906
re: tbl_time, in mine I also have columns for fiscal month, fiscal week, fiscal quarter, calendar month, calendar week, holiday (yes/no), which can be real darn handy.
0
 
LVL 14

Expert Comment

by:sathyagiri
ID: 17084184
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 2000 total points
ID: 17084312

Try:

select * from the_table
where to_char(add_months(trunc(the_date),+3),'YYYY') = to_char(trunc(sysdate),'yyyy')

Gives current FY.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 17084334

PS: If sysdate within FY, otherwise, hard-code the FY:

select * from the_table
where to_char(add_months(trunc(the_date),+3),'YYYY') = '2006'

0
 
LVL 1

Expert Comment

by:davetheravesmith
ID: 17085246
Chaps,

My fiscal year is April to March, so I just use logic like this...

if month(date) < 4 then year(date) -1 else year (date)

What this does is that if the month of the date fall in Jan Feb or Mar, it's Fyear is one less than the real year, otherwise it is the same.

Very simple logic / very simply fomula

Regards

DAVE S
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 17085351

Depends on the particular definition of fiscal year, for example:

If fiscal year is Oct'05 - Sept'06, then you have to ADD 3 months to the date to get the actual fiscal year (which would be: 2006).

Example:

to_char(add_months(trunc(the_date),+3),'YYYY')





 
0
 
LVL 1

Expert Comment

by:davetheravesmith
ID: 17089423
In my case the fiscal year takes the year of the start month, hence Jan Feb & Mar this year (2006) belong to fiscal year 2005.

Just a question, why all the to-char business?  Wouldn't it be sufficient to just display the numeric returned by the YEAR function?

Regards

DAVE S
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 17090624

Then you do need to subtract 9 from the date:

to_char(add_months(trunc(the_date),-9),'YYYY')

the to_char is to convert the DATE to CHAR depending on th eformat supplied YYYY.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 17090650

Are you sure of the fiscal year designation?

Normally fiscal years beginning in the first semester are named after the year of beginning month and fy beginning in second semester are normally named after the ending month's year.

But who know's? Every other company may follow it's own rules.
0
 

Author Comment

by:lcor
ID: 17141039
MikeOM_DBA's is the best solution..works everytime...thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question