Solved

Need sql help to caluculate fiscal year based on todays date

Posted on 2013-02-06
7
721 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL - Rotating Values in SQL 9 53
MS SQL Inner Join - Multiple Join Parameters 2 23
TSQL - IF ELSE? 3 29
SQL Server stored proc 2 13
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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

895 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

12 Experts available now in Live!

Get 1:1 Help Now