Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Fiscal Calendar month

$
0
0

I am trying to create a function within my select statement that will return the fiscal month number. My company's fiscal calendar starts on October 1. The first problem I had is I wasn't sure if there was any easy way to just say if the current month is X return Y. Instead, I had to use the full date, because the function I knew of would just say "When date is between A and B..." The problem there is I will have to update it every so often with new years, and removing past years so as to avoid the query getting too large. Is there any way to instead just use the month? Then, I could just say "If current month is X return Y," and it wouldn't matter the year. Either way, below is what I used. When I try to preview it in Visual Studio 2008, I am getting an error saying "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."

I've seen some questions here regarding fiscal calendars in the past, but I've never understood the answers people have given. I have found that a little confusing, so I was hoping this simpler (even if not necessarily ideal) solution I found would help. Unfortunately, I am getting an error, so obviously it did not. Does anybody have any advice they can offer to get this to work?

Select
CASE
WHEN GETDATE() Between '20131001 00:00:00' AND '20131031 11:59:59' THEN '1'
WHEN GETDATE() Between '20131101 00:00:00' AND '20131131 11:59:59' THEN '2'
WHEN GETDATE() Between '20131201 00:00:00' AND '20131231 11:59:59' THEN '3'
WHEN GETDATE() Between '20140101 00:00:00' AND '20140131 11:59:59' THEN '4'
WHEN GETDATE() Between '20140201 00:00:00' AND '20140228 11:59:59' THEN '5'
WHEN GETDATE() Between '20140301 00:00:00' AND '20140331 11:59:59' THEN '6'
WHEN GETDATE() Between '20140401 00:00:00' AND '20140430 11:59:59' THEN '7'
WHEN GETDATE() Between '20140501 00:00:00' AND '20140531 11:59:59' THEN '8'
WHEN GETDATE() Between '20140601 00:00:00' AND '20140630 11:59:59' THEN '9'
WHEN GETDATE() Between '20140701 00:00:00' AND '20140731 11:59:59' THEN '10'
WHEN GETDATE() Between '20140801 00:00:00' AND '20140831 11:59:59' THEN '11'
WHEN GETDATE() Between '20140901 00:00:00' AND '20140930 11:59:59' THEN '12'
WHEN GETDATE() Between '20141001 00:00:00' AND '20141031 11:59:59' THEN '1'
WHEN GETDATE() Between '20141101 00:00:00' AND '20141131 11:59:59' THEN '2'
WHEN GETDATE() Between '20141201 00:00:00' AND '20141231 11:59:59' THEN '3'
WHEN GETDATE() Between '20150101 00:00:00' AND '20150131 11:59:59' THEN '4'
WHEN GETDATE() Between '20150201 00:00:00' AND '20150228 11:59:59' THEN '5'
WHEN GETDATE() Between '20150301 00:00:00' AND '20150331 11:59:59' THEN '6'
WHEN GETDATE() Between '20150401 00:00:00' AND '20150430 11:59:59' THEN '7'
WHEN GETDATE() Between '20150501 00:00:00' AND '20150531 11:59:59' THEN '8'
WHEN GETDATE() Between '20150601 00:00:00' AND '20150630 11:59:59' THEN '9'
WHEN GETDATE() Between '20150701 00:00:00' AND '20150731 11:59:59' THEN '10'
WHEN GETDATE() Between '20150801 00:00:00' AND '20150831 11:59:59' THEN '11'
WHEN GETDATE() Between '20150901 00:00:00' AND '20150930 11:59:59' THEN '12'
WHEN GETDATE() Between '20151001 00:00:00' AND '20151031 11:59:59' THEN '1'
WHEN GETDATE() Between '20151101 00:00:00' AND '20151131 11:59:59' THEN '2'
WHEN GETDATE() Between '20151201 00:00:00' AND '20151231 11:59:59' THEN '3'
WHEN GETDATE() Between '20160101 00:00:00' AND '20160131 11:59:59' THEN '4'
WHEN GETDATE() Between '20160201 00:00:00' AND '20160228 11:59:59' THEN '5'
WHEN GETDATE() Between '20160301 00:00:00' AND '20160331 11:59:59' THEN '6'
WHEN GETDATE() Between '20160401 00:00:00' AND '20160430 11:59:59' THEN '7'
WHEN GETDATE() Between '20160501 00:00:00' AND '20160531 11:59:59' THEN '8'
WHEN GETDATE() Between '20160601 00:00:00' AND '20160630 11:59:59' THEN '9'
WHEN GETDATE() Between '20160701 00:00:00' AND '20160731 11:59:59' THEN '10'
WHEN GETDATE() Between '20160801 00:00:00' AND '20160831 11:59:59' THEN '11'
WHEN GETDATE() Between '20160901 00:00:00' AND '20160930 11:59:59' THEN '12' END
FiscalMonthNbr




Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>