Solved

Set Fiscal start and end dates based on today's date

Posted on 2014-09-10
6
647 Views
Last Modified: 2015-09-22
I have a query that grabs an employee's hours for the current fiscal year based on the GetDate().  But I realized that it will only work between May and December of the current year.  Our fiscal year is May 1 - April 30.  So if the GetDate() is September 10, 2014 then the StartDate and EndDate would be 5/1/2014 12:00:00 AM and 4/30/2015 12:00:00 AM respectively.  But if GetDate() is February 15, 2015 (if it is run approximately 5 months from now) then YEAR(GETDATE()) will return 2015 and the StartDate would then be set at 5/1/2015 12:00:00 AM.  I've tried modifying the code but I can't seem to get it to work.  Since I don't want to wait until February 15th to test the code I tried putting a parameter or string with a date set after January 1st but I keep getting errors about the object type.  

If anyone can help me figure out how to put a CASE in this code to test if GetDate() is > May 1 it would be greatly appreciated!!

SELECT     WempID AS EmployeeID, WCltID AS ProjectID, WCltName AS ProjectName, Whours AS HoursWorked, CAST(RTRIM('May 1, ' + CAST(YEAR(GETDATE()) AS char)) 
                      AS DateTime) AS StartDate, CAST(RTRIM('April 30, ' + CAST(YEAR(GETDATE()) + 1 AS char)) AS DateTime) AS EndDate
FROM         WIP
WHERE     (WempID = @CurrentUser) AND (Wdate BETWEEN CAST(RTRIM('May 1, ' + CAST(YEAR(GETDATE()) AS char)) AS DateTime) AND 
                      CAST(RTRIM('April 30, ' + CAST(YEAR(GETDATE()) + 1 AS char)) AS DateTime))

Open in new window

0
Comment
Question by:HSI_guelph
6 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 300 total points
ID: 40315603
CAST(RTRIM('May 1, ' + CAST(YEAR(GETDATE()) + CASE WHEN MONTH(GETDATE()) < 5 THEN -1 ELSE 0 END AS char))  AS DATETIME)

and

CAST(RTRIM('April 30, ' + CAST(YEAR(GETDATE()) + CASE WHEN MONTH(GETDATE()) < 5 THEN 0 ELSE 1 END AS char)) AS DATETIME)
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 200 total points
ID: 40315839
Do NOT use BETWEEN for this.

Do it like this:
FROM         WIP
WHERE    ...
AND Wdate >= '20130501'
AND Wdate  < '20140501'

And, please oh please, do NOT rely on language when dealing with dates (e.g. "May" is English) .

For versions of SQL Server without datefromparts():
select
    convert(varchar(23),Wdate,121) as Wdate
from WIP
where Wdate >= case when month(getdate()) < 5 then 
                    cast(convert(varchar(4),year(getdate()) - 1) + '0501' as datetime)
                else 
                    cast(convert(varchar(4),year(getdate())) + '0501' as datetime)
                end
and   Wdate <  case when month(getdate()) < 5 then 
                    dateadd(year,1,cast(convert(varchar(4),year(getdate()) - 1) + '0501' as datetime))
                else 
                    dateadd(year,1,cast(convert(varchar(4),year(getdate())) + '0501' as datetime))
                end
order by Wdate
;

Open in new window


For versions of SQL Server with datefromparts():
select
    convert(varchar(23),Wdate,121) as Wdate
from WIP
where Wdate >= case when month(getdate()) < 5 then 
                    datefromparts(year(getdate()) - 1 ,5,1)
                else 
                    datefromparts(year(getdate()) ,5,1)
                end
and   Wdate <  case when month(getdate()) < 5 then 
                    datefromparts(year(getdate()) ,5,1)
                else 
                    datefromparts(year(getdate()) + 1 ,5,1)
                end
order by Wdate
;

Open in new window


I have setup a demonstration for you, HERE, please take particular note of the 30th of April dates and TIME

for more about between please see: "Beware of Between"

Setup details of that demonstration:
    
    
    CREATE TABLE WIP
    	([Wdate] datetime)
    ;
    	
    INSERT INTO WIP
    	([Wdate])
    VALUES
    	('2012-12-31 00:00:00'),
    	('2013-01-01 00:00:00'),
    	('2013-01-31 00:00:00'),
    	('2013-02-01 00:00:00'),
    	('2013-02-28 00:00:00'),
    	('2013-03-01 00:00:00'),
    	('2013-03-31 00:00:00'),
    	('2013-04-01 00:00:00'),
    	('2013-04-30 00:00:00'),
    	('2013-05-01 00:00:00'),
    	('2013-05-31 00:00:00'),
    	('2013-06-01 00:00:00'),
    	('2013-06-30 00:00:00'),
    	('2013-07-01 00:00:00'),
    	('2013-07-31 00:00:00'),
    	('2013-08-01 00:00:00'),
    	('2013-08-31 00:00:00'),
    	('2013-09-01 00:00:00'),
    	('2013-09-30 00:00:00'),
    	('2013-10-01 00:00:00'),
    	('2013-10-31 00:00:00'),
    	('2013-11-01 00:00:00'),
    	('2013-11-30 00:00:00'),
    	('2013-12-01 00:00:00'),
    	('2013-12-31 00:00:00'),
    	('2014-01-01 00:00:00'),
    	('2014-01-31 00:00:00'),
    	('2014-02-01 00:00:00'),
    	('2014-02-28 00:00:00'),
    	('2014-03-01 00:00:00'),
    	('2014-03-31 00:00:00'),
    	('2014-04-01 00:00:00'),
    	('2014-04-30 00:00:00'),
    	('2014-05-01 00:00:00'),
    	('2014-05-31 00:00:00'),
    	('2014-06-01 00:00:00'),
    	('2014-06-30 00:00:00'),
    	('2014-07-01 00:00:00'),
    	('2014-07-31 00:00:00'),
    	('2014-08-01 00:00:00'),
    	('2014-08-31 00:00:00'),
    	('2014-09-01 00:00:00'),
    	('2014-09-30 00:00:00'),
    	('2014-10-01 00:00:00'),
    	('2014-10-31 00:00:00'),
    	('2014-11-01 00:00:00'),
    	('2014-11-30 00:00:00'),
    	('2014-12-01 00:00:00'),
    	('2014-12-31 00:00:00'),
    	('2015-01-01 00:00:00'),
    	('2015-01-31 00:00:00'),
    	('2015-02-01 00:00:00'),
    	('2015-02-28 00:00:00'),
    	('2015-03-01 00:00:00'),
    	('2015-03-31 00:00:00'),
    	('2015-04-01 00:00:00'),
    	('2015-04-30 00:00:00'),
    	('2015-05-01 00:00:00'),
    	('2015-05-31 00:00:00'),
    	('2015-06-01 00:00:00'),
    	('2015-06-30 00:00:00'),
    	('2015-07-01 00:00:00'),
    	('2015-07-31 00:00:00'),
    	('2015-08-01 00:00:00'),
    	('2015-08-31 00:00:00'),
    	('2015-09-01 00:00:00'),
    	('2015-09-30 00:00:00'),
    	('2015-10-01 00:00:00'),
    	('2015-10-31 00:00:00'),
    	('2015-11-01 00:00:00'),
    	('2015-11-30 00:00:00'),
    	('2015-12-01 00:00:00'),
    	('2015-12-31 00:00:00'),
    	('2016-01-01 00:00:00')
    ;

**Query 1**:

    update WIP
    set Wdate = dateadd(millisecond,-4,dateadd(day,1,Wdate))
    where day(Wdate) <> 1
    

**[Results][2]**:
    

**Query 2**:

    select
        convert(varchar(23),Wdate,121) as Wdate
    from WIP
    where Wdate >= case when month(getdate()) < 5 then 
                        cast(convert(varchar(4),year(getdate()) - 1) + '0501' as datetime)
                    else 
                        cast(convert(varchar(4),year(getdate())) + '0501' as datetime)
                    end
    and   Wdate <  case when month(getdate()) < 5 then 
                        dateadd(year,1,cast(convert(varchar(4),year(getdate()) - 1) + '0501' as datetime))
                    else 
                        dateadd(year,1,cast(convert(varchar(4),year(getdate())) + '0501' as datetime))
                    end
    order by Wdate
    

**[Results][3]**:
    
    |                   WDATE |
    |-------------------------|
    | 2014-05-01 00:00:00.000 |
    | 2014-05-31 23:59:59.997 |
    | 2014-06-01 00:00:00.000 |
    | 2014-06-30 23:59:59.997 |
    | 2014-07-01 00:00:00.000 |
    | 2014-07-31 23:59:59.997 |
    | 2014-08-01 00:00:00.000 |
    | 2014-08-31 23:59:59.997 |
    | 2014-09-01 00:00:00.000 |
    | 2014-09-30 23:59:59.997 |
    | 2014-10-01 00:00:00.000 |
    | 2014-10-31 23:59:59.997 |
    | 2014-11-01 00:00:00.000 |
    | 2014-11-30 23:59:59.997 |
    | 2014-12-01 00:00:00.000 |
    | 2014-12-31 23:59:59.997 |
    | 2015-01-01 00:00:00.000 |
    | 2015-01-31 23:59:59.997 |
    | 2015-02-01 00:00:00.000 |
    | 2015-02-28 23:59:59.997 |
    | 2015-03-01 00:00:00.000 |
    | 2015-03-31 23:59:59.997 |
    | 2015-04-01 00:00:00.000 |
    | 2015-04-30 23:59:59.997 |


**Query 3**:

    select
        convert(varchar(23),Wdate,121) as Wdate
    from WIP
    where Wdate >= case when month(getdate()) < 5 then 
                        datefromparts(year(getdate()) - 1 ,5,1)
                    else 
                        datefromparts(year(getdate()) ,5,1)
                    end
    and   Wdate <  case when month(getdate()) < 5 then 
                        datefromparts(year(getdate()) ,5,1)
                    else 
                        datefromparts(year(getdate()) + 1 ,5,1)
                    end
    order by Wdate
    

**[Results][4]**:
    
    |                   WDATE |
    |-------------------------|
    | 2014-05-01 00:00:00.000 |
    | 2014-05-31 23:59:59.997 |
    | 2014-06-01 00:00:00.000 |
    | 2014-06-30 23:59:59.997 |
    | 2014-07-01 00:00:00.000 |
    | 2014-07-31 23:59:59.997 |
    | 2014-08-01 00:00:00.000 |
    | 2014-08-31 23:59:59.997 |
    | 2014-09-01 00:00:00.000 |
    | 2014-09-30 23:59:59.997 |
    | 2014-10-01 00:00:00.000 |
    | 2014-10-31 23:59:59.997 |
    | 2014-11-01 00:00:00.000 |
    | 2014-11-30 23:59:59.997 |
    | 2014-12-01 00:00:00.000 |
    | 2014-12-31 23:59:59.997 |
    | 2015-01-01 00:00:00.000 |
    | 2015-01-31 23:59:59.997 |
    | 2015-02-01 00:00:00.000 |
    | 2015-02-28 23:59:59.997 |
    | 2015-03-01 00:00:00.000 |
    | 2015-03-31 23:59:59.997 |
    | 2015-04-01 00:00:00.000 |
    | 2015-04-30 23:59:59.997 |


**Query 4**:

    select
        FY
      , FYStart
      , convert(varchar(23),Wdate,121)
      , dateadd(year,1,FYStart) as FYEnd
    from WIP
    cross apply (
    
                select
                    case when month(Wdate) < 5 then 
                        year(Wdate) - 1
                    else 
                        year(Wdate)
                    end as FY
                
                  , case when month(Wdate) < 5 then 
                        cast(convert(varchar(4),year(Wdate) - 1) + '0501' as date)
                    else 
                        cast(convert(varchar(4),year(Wdate)) + '0501' as date)
                    end as FYstart  
                  
                   ) ca (FY, FYStart)
    

**[Results][5]**:
    
    |   FY |    FYSTART |                COLUMN_2 |      FYEND |
    |------|------------|-------------------------|------------|
    | 2012 | 2012-05-01 | 2012-12-31 23:59:59.997 | 2013-05-01 |
    | 2012 | 2012-05-01 | 2013-01-01 00:00:00.000 | 2013-05-01 |
    | 2012 | 2012-05-01 | 2013-01-31 23:59:59.997 | 2013-05-01 |
    | 2012 | 2012-05-01 | 2013-02-01 00:00:00.000 | 2013-05-01 |
    | 2012 | 2012-05-01 | 2013-02-28 23:59:59.997 | 2013-05-01 |
    | 2012 | 2012-05-01 | 2013-03-01 00:00:00.000 | 2013-05-01 |
    | 2012 | 2012-05-01 | 2013-03-31 23:59:59.997 | 2013-05-01 |
    | 2012 | 2012-05-01 | 2013-04-01 00:00:00.000 | 2013-05-01 |
    | 2012 | 2012-05-01 | 2013-04-30 23:59:59.997 | 2013-05-01 |
    | 2013 | 2013-05-01 | 2013-05-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-05-31 23:59:59.997 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-06-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-06-30 23:59:59.997 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-07-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-07-31 23:59:59.997 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-08-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-08-31 23:59:59.997 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-09-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-09-30 23:59:59.997 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-10-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-10-31 23:59:59.997 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-11-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-11-30 23:59:59.997 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-12-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2013-12-31 23:59:59.997 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2014-01-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2014-01-31 23:59:59.997 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2014-02-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2014-02-28 23:59:59.997 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2014-03-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2014-03-31 23:59:59.997 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2014-04-01 00:00:00.000 | 2014-05-01 |
    | 2013 | 2013-05-01 | 2014-04-30 23:59:59.997 | 2014-05-01 |
    | 2014 | 2014-05-01 | 2014-05-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-05-31 23:59:59.997 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-06-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-06-30 23:59:59.997 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-07-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-07-31 23:59:59.997 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-08-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-08-31 23:59:59.997 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-09-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-09-30 23:59:59.997 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-10-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-10-31 23:59:59.997 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-11-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-11-30 23:59:59.997 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-12-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2014-12-31 23:59:59.997 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2015-01-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2015-01-31 23:59:59.997 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2015-02-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2015-02-28 23:59:59.997 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2015-03-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2015-03-31 23:59:59.997 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2015-04-01 00:00:00.000 | 2015-05-01 |
    | 2014 | 2014-05-01 | 2015-04-30 23:59:59.997 | 2015-05-01 |
    | 2015 | 2015-05-01 | 2015-05-01 00:00:00.000 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-05-31 23:59:59.997 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-06-01 00:00:00.000 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-06-30 23:59:59.997 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-07-01 00:00:00.000 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-07-31 23:59:59.997 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-08-01 00:00:00.000 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-08-31 23:59:59.997 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-09-01 00:00:00.000 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-09-30 23:59:59.997 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-10-01 00:00:00.000 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-10-31 23:59:59.997 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-11-01 00:00:00.000 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-11-30 23:59:59.997 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-12-01 00:00:00.000 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2015-12-31 23:59:59.997 | 2016-05-01 |
    | 2015 | 2015-05-01 | 2016-01-01 00:00:00.000 | 2016-05-01 |



  [1]: http://sqlfiddle.com/#!6/6b37d/1

  [2]: http://sqlfiddle.com/#!6/6b37d/1/0

  [3]: http://sqlfiddle.com/#!6/6b37d/1/1

  [4]: http://sqlfiddle.com/#!6/6b37d/1/2

  [5]: http://sqlfiddle.com/#!6/6b37d/1/3

Open in new window

date format styles: SQL Server Date Styles (formats) using CONVERT()
0
 

Author Closing Comment

by:HSI_guelph
ID: 40317003
Thank you both for your help!  Shaun Kline's solution worked and was first but I had no idea about the BETWEEN issue until PortletPaul posted it.  So you both helped build my query.  Thank you!
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:HSI_guelph
ID: 40317020
Final Code

SELECT     WempID AS EmployeeID, WCltID AS ProjectID, WCltName AS ProjectName, Whours AS HoursWorked, CAST(RTRIM('May 1, ' + CAST(YEAR(GETDATE()) 
                      + CASE WHEN MONTH(GetDate()) < 5 THEN - 1 ELSE 0 END AS char)) AS DATETIME) AS StartDate, CAST(RTRIM('April 30, ' + CAST(YEAR(GETDATE()) 
                      + CASE WHEN MONTH(GetDate()) < 5 THEN 0 ELSE 1 END AS char)) AS DATETIME) AS EndDate
FROM         WIP
WHERE     (WempID = @CurrentUser) AND (Wdate >= CAST(RTRIM('May 1, ' + CAST(YEAR(GETDATE()) + CASE WHEN MONTH(GetDate()) < 5 THEN - 1 ELSE 0 END AS char)) 
                      AS DATETIME)) AND (Wdate < CAST(RTRIM('April 30, ' + CAST(YEAR(GETDATE()) + CASE WHEN MONTH(GetDate()) < 5 THEN 0 ELSE 1 END AS char)) AS DATETIME))

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40318358
That final query is wrong!

now you have excluded April 30th completely

You need to use "May 1" for BOTH the start and the end,

But I do mean this most sincerely, you should not use language based strings for this.

Try reading this:
The ultimate guide to the datetime datatypes

and this:
"Bad habits to kick : mis-handling date / range queries"

The safest possible date literal format in SQL Server is 'YYYYMMDD'

and it isn't a big ask for you to stop using "May 1" and use "0501" instead.

---
I am very pleased you learned about the dangers of between, but you must also remember to move up the higher date
(i.e. where you once used April 30 you add 1 day and now use May 1, but as numbers not month names)
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40988990
HSI - 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

929 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

10 Experts available now in Live!

Get 1:1 Help Now