Solved

Need sql help to caluculate fiscal year based on todays date

Posted on 2013-02-06
7
715 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:ScottPletcher
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 69

Accepted Solution

by:
ScottPletcher 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

Backup Your Microsoft Windows Server®

Backup 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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 123
C# SQL BULK INSERT CLASS 5 36
Calculating Business Hours 19 65
SQL Inner Join Vs SubQueries 9 26
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

747 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

9 Experts available now in Live!

Get 1:1 Help Now