SQL - 在自定义开始日期生成双周结束日期

时间:2017-02-22 09:57:54

标签: sql function tsql date datetime

我希望根据给定的开始日期获得定制的Fort Night时段。 我有一张看起来像这样的表:

IF OBJECT_ID('tempdb..#tbl1') IS NOT NULL DROP TABLE #tbl1
SET DATEFIRST 1
DECLARE @StartDateTime DATETIME
DECLARE @EndDateTime DATETIME

SET @StartDateTime = '2016-09-03'
SET @EndDateTime = '2017-01-28';

WITH DateRange(DateData) AS 
(
    SELECT @StartDateTime as Date
    UNION ALL
    SELECT DATEADD(d,1,DateData)
    FROM DateRange 
    WHERE DateData < @EndDateTime
)
SELECT ROW_NUMBER() OVER(ORDER BY DateData ASC) As ROWNum,DateData AS Date1
into  #tbl1
FROM DateRange
OPTION (MAXRECURSION 0)
GO

SELECT top 10 * FROM #tbl1

         Date1
        2016-09-09 00:00:00.000
        2016-09-10 00:00:00.000
        2016-09-11 00:00:00.000
        2016-09-12 00:00:00.000
        2016-09-13 00:00:00.000
        2016-09-14 00:00:00.000
        2016-09-15 00:00:00.000
        2016-09-16 00:00:00.000
        2016-09-17 00:00:00.000
        2016-09-18 00:00:00.000
        2016-09-19 00:00:00.000
        2016-09-20 00:00:00.000
        2016-09-21 00:00:00.000
        2016-09-22 00:00:00.000

我想说我的双周期的第一个日期是2016-09-09,它结束于2016-09-22。如何获得每个日期的双周结束日期。

所以我希望它看起来像

         Date1                           FortNightEndDate
        2016-09-09 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-10 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-11 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-12 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-13 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-14 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-15 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-16 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-17 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-18 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-19 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-20 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-21 00:00:00.000       2016-09-22 00:00:00.000
        2016-09-22 00:00:00.000       2016-09-22 00:00:00.000

我正在使用SQL Server 2005。

解答: 我能够使用以下代码解决它。基本上我只创建了3个表: StartDates EndDates InbetweenDates

Start / EndDates表只有我2周时间的开始和结束以及ID(行号) InbetweenDates表具有2个日期之间的所有日期,并且还有一个ID列,但不是每行上升1,而是每14行增加1。 然后我加入了3张桌子。本质上,Start / EndDates表是查找表。 我从here开始每隔14天获得一次RowNumber。

-- BEtween Dates
IF OBJECT_ID('tempdb..#BetweenDates') IS NOT NULL DROP TABLE #BetweenDates
DECLARE @StartDateTime DATETIME
DECLARE @EndDateTime DATETIME

SET @StartDateTime = '2016-09-09'
SET @EndDateTime = '2017-04-30';

WITH DateRange(DateData) AS 
(
    SELECT @StartDateTime as Date
    UNION ALL
    SELECT DATEADD(d,1,DateData)
    FROM DateRange 
    WHERE DateData < @EndDateTime
)
SELECT 
    DateData
into  #BetweenDates
FROM DateRange
OPTION (MAXRECURSION 0)
GO



select 
    (case when convert(int, (ROW_NUMBER() OVER (Order by (select 0)) % 14))=0 then 0 else 1 end) 
    + convert(int, (ROW_NUMBER() OVER (Order by (select 0)) / 14)) as ID
    ,DateData
INTO #BetweenDates_ID
from #BetweenDates


-- Start Dates

IF OBJECT_ID('tempdb..#StartDates') IS NOT NULL DROP TABLE #StartDates
DECLARE @StartDateTime DATETIME
DECLARE @EndDateTime DATETIME

SET @StartDateTime = '2016-09-09'
SET @EndDateTime = '2017-04-30';

WITH DateRange(DateData) AS 
(
    SELECT @StartDateTime as Date
    UNION ALL
    SELECT DATEADD(d,14,DateData)
    FROM DateRange 
    WHERE DateData < @EndDateTime
)
SELECT ROW_NUMBER() OVER(ORDER BY DateData ASC) As ID,DateData
into  #StartDates
FROM DateRange
OPTION (MAXRECURSION 0)
GO

-- End Dates

IF OBJECT_ID('tempdb..#EndDates') IS NOT NULL DROP TABLE #EndDates
DECLARE @StartDateTime DATETIME
DECLARE @EndDateTime DATETIME

SET @StartDateTime = '2016-09-22'
SET @EndDateTime = '2017-04-30';

WITH DateRange(DateData) AS 
(
    SELECT @StartDateTime as Date
    UNION ALL
    SELECT DATEADD(d,14,DateData)
    FROM DateRange 
    WHERE DateData < @EndDateTime
)
SELECT ROW_NUMBER() OVER(ORDER BY DateData ASC) As ID,DateData
into  #EndDates
FROM DateRange
OPTION (MAXRECURSION 0)
GO


--SELECT * FROM #StartDates
--SELECT * FROM #EndDates
--SELECT * FROM #BetweenDates_ID

SELECT 
    st.DateData AS StartDate
    ,ed.DateData AS EndDate
    ,bd.DateData AS BetweenDate
FROM 
    #StartDates st
    JOIN
    #EndDates ed
    ON st.ID = ed.ID
    LEFT JOIN
    #BetweenDates_ID bd
    ON st.ID = bd.ID

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT Date1,
       DATEADD(DD, -1, DATEADD(WW,2,Date1)) AS FortNightEndDate
FROM #tbl1