?
Solved

Need sql help to caluculate fiscal year based on todays date

Posted on 2013-02-06
7
Medium Priority
?
750 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 1600 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 49

Assisted Solution

by:PortletPaul
PortletPaul earned 400 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 66

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

764 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