Solved

Date falls within Fiscal Year Query

Posted on 2006-07-11
15
749 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 65

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
 
LVL 92

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 65

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 65

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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now