PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 26th, 2012, 09:46 PM
slamdunkrookie slamdunkrookie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 13 slamdunkrookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 23 m 50 sec
Reputation Power: 0
Select Financial year

How to write a select query for financial year? The financial is from 1st July to 30th June each year. Here's the pseudo code (dd-mm-yyyy):

If extract(month from current_date) < 7 then

select date between ('01-07-date_part('year', current_date)'-1) and ('30-06-date_part('year', current_date')

else

select date between ('01-07-date_part('year', current_date)') and ('30-06-date_part('year', current_date')

End if

Appreciate any help.

Reply With Quote
  #2  
Old May 1st, 2012, 02:28 AM
slamdunkrookie slamdunkrookie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 13 slamdunkrookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 23 m 50 sec
Reputation Power: 0
anybody?

Reply With Quote
  #3  
Old May 22nd, 2012, 04:41 PM
joeflinthiggins joeflinthiggins is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 14 joeflinthiggins User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 20 m 33 sec
Reputation Power: 0
Hello,

If you are trying to simply return the financial year (July 1st through June 30th) from a date column, then this should give you what you need:

select
case
when to_char(now(), 'mm')::int >= 7
then to_char(now(), 'yyyy')::int +1
else to_char(now(), 'yyyy')::int
end

You can replace now() with your date column.

Let me know if I've misunderstood your end goal.

Regards,

Joe

Reply With Quote
  #4  
Old May 24th, 2012, 02:53 PM
FlyingBeetroot FlyingBeetroot is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 73 FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 24 m 52 sec
Reputation Power: 35
Facebook
Call me foolish, but I hate hard coding this type of thing into queries.

Consider this approach:

Code:
CREATE TABLE financial_calendar (
  financial_year character varying,
  year_effective_from date,
  year_effective_to date)
-- put a primary key on financial_year

populate this table backwards 10 years, and forwards 100 (some ridiculous number it will never reach).

Table date should look something like this:
SELECT * FROM financial_calendar;

--
financial_year | year_effective_from | year_effective_to
2007                 01/07/2006                30/06/2007
2008                 01/07/2007                30/06/2008
.....

Then your query becomes

SELECT
  financial_year
FROM
  financial_calendar
WHERE
  now() between year_effective_from AND year_effective_to


Pros:
- If you ever want this to extend to other countries; you just re-populate the table with the relevant fin year data
- Fast query; no internal functions (casting), and no chance of screwing up the query.

Cons:
- Another table (which is really here nor there)
- You might populate it incorrectly

I do this where I know the data is static; but not consistent in all use cases (in this example: fin year July->June in Australia, but America it's Oct->sept

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Select Financial year

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap