Solved

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

Posted on 2014-09-10
6
639 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 25

Accepted Solution

by:
Shaun Kline earned 300 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:HSI_guelph
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

772 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