Solved

Need sql help to caluculate fiscal year based on todays date

Posted on 2013-02-06
7
735 Views
Last Modified: 2015-09-22
I need help in caluculating fiscal year based on a meetingdate as well the date when the user is running the query

like I have the formula to caluculate the fiscal years based on meetingdate as below:

begin
declare @meetingdate as varchar(20)
set @meetingdate='02/20/2006'
print YEAR(DATEADD(month, 6 + DATEDIFF(month, 0,
@meetingdate), 0))
end
 
and the result is 2006 and now the user wants to compae this with the date he's running the query say for example if he's running the above query after the fiscalyear which is on 07/01/2013 then the result of the fiscalyear should be 2007 and if he's running it before like 6/13/2013 then the result should be 2006.He wants the results based on the fiscalyears of the meetingdate but compare with when he's running the query also.  I hope I explained it well if not I can kindly let me know.
0
Comment
Question by:welcome 123
7 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 38861751
begin
declare @meetingdate as varchar(20)
set @meetingdate='02/20/2006'
print YEAR(@meetingdate) + CASE WHEN MONTH(getdate()) >=7 THEN 1 ELSE 0 END
end


Or, to avoid a CASE (or IF):

print YEAR(@meetingdate) + ((MONTH(getdate()) + 6) / 12 )
0
 

Author Comment

by:welcome 123
ID: 38861803
begin
declare @meetingdate as varchar(20)
declare @getdate as varchar(20)
set @meetingdate='02/27/2012'
set @getdate='06/27/2012'
print YEAR(@meetingdate) + ((MONTH(@getdate) + 6) / 12 )
end

I get 2013 where I need to get this as 2012 as fiscalyear starts with 07/01
0
 

Author Comment

by:welcome 123
ID: 38861811
but using your case statement worked. I am just using what you sent without using my brain which I should I guess:-)

begin
declare @meetingdate as varchar(20)
declare @getdate as varchar(20)
set @meetingdate='02/27/2012'
set @getdate='06/27/2012'
print YEAR(@meetingdate) + CASE WHEN MONTH(@getdate) >=7 THEN 1 ELSE 0 END
end

result 2012

also I am using getdate as a variable for testing for the while
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 400 total points
ID: 38861973
>> print YEAR(@meetingdate) + ((MONTH(@getdate) + 6) / 12 )
end

I get 2013 where I need to get this as 2012 as fiscalyear starts with 07/01
<<

Good point.

print YEAR(@meetingdate) + ((MONTH(@getdate) + 5) / 12 )
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 100 total points
ID: 38862504
year(<<your-date>>) + (month(<<your-date>>) / 7) as [Fiscal Yr]

with cte as (
       /* just to produce a range of dates */
	select 1 as mnths, getdate() as dt
	union all 
	select mnths + 1 , dateadd(month,(mnths+1),getdate())
    from cte 
	where mnths <= 18
)

select
  cte.mnths
, cte.dt
, convert(varchar(10), cte.dt, 102)  as [yyyy.mm.dd]
, year(cte.dt) + (month(cte.dt) / 7) as [Fiscal Yr]
from cte

Open in new window

 mnths     dt                    yyyy.mm.dd     Fiscal Yr    
 --------  --------------------  -------------  ----------- 
 1         2/6/2013 7:45:18 PM   2013.02.06     2013        
 2         4/6/2013 7:45:18 PM   2013.04.06     2013        
 3         5/6/2013 7:45:18 PM   2013.05.06     2013        
 4         6/6/2013 7:45:18 PM   2013.06.06     2013        
 5         7/6/2013 7:45:18 PM   2013.07.06     2014        
 6         8/6/2013 7:45:18 PM   2013.08.06     2014        
 7         9/6/2013 7:45:18 PM   2013.09.06     2014        
 8         10/6/2013 7:45:18 PM  2013.10.06     2014        
 9         11/6/2013 7:45:18 PM  2013.11.06     2014        
 10        12/6/2013 7:45:18 PM  2013.12.06     2014        
 11        1/6/2014 7:45:18 PM   2014.01.06     2014        
 12        2/6/2014 7:45:18 PM   2014.02.06     2014        
 13        3/6/2014 7:45:18 PM   2014.03.06     2014        
 14        4/6/2014 7:45:18 PM   2014.04.06     2014        
 15        5/6/2014 7:45:18 PM   2014.05.06     2014        
 16        6/6/2014 7:45:18 PM   2014.06.06     2014        
 17        7/6/2014 7:45:18 PM   2014.07.06     2015        
 18        8/6/2014 7:45:18 PM   2014.08.06     2015        
 19        9/6/2014 7:45:18 PM   2014.09.06     2015 

Open in new window

0
 

Author Closing Comment

by:welcome 123
ID: 38869015
Sorry had to give only 100 points to
By: PortletPaul as I always belive in first expert correct answer. But thanks to all
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40989059
sailaja - In case this helps, I just kicked out an article that deals specifically with Fiscal calendar planning --> SQL Server Calendar Table:  Fiscal Years.   Let me know if this helps you, and if yes please click on the 'Good Article' button and provide some feedback.  Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

839 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