Need sql help to caluculate fiscal year based on todays date

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.
welcome 123Asked:
Who is Participating?
 
Scott PletcherConnect With a Mentor Senior DBACommented:
>> 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
 
Scott PletcherSenior DBACommented:
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
 
welcome 123Author Commented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
welcome 123Author Commented:
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
 
PortletPaulConnect With a Mentor freelancerCommented:
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
 
welcome 123Author Commented:
Sorry had to give only 100 points to
By: PortletPaul as I always belive in first expert correct answer. But thanks to all
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.