我如何在 sql 服务器中显示从 StartDate 开始的结束日期?

How do i display the end date from StartDate in sql server?

我正在使用 SQL SERVER 2008。我有一个名为 Cut-Off table 的 table。此 table 仅显示每个月的截止日期 StartDate。所以我正在创建一个临时 table 来存储 StartDateEndDateMonthYear。但我不知道如何用 datetime 获得 EndDate

TABLE

----------------------------------------------------------
StartDate              EndDate     Month   Year
----------------------------------------------------------
2014-12-15 00:00:00     NULL        12      2014
2015-01-26 00:00:00     NULL         1      2015
2015-02-26 00:00:00     NULL         2      2015
---------------------------------------------------------

预期输出

--------------------------------------------------------------------
    StartDate              EndDate                   Month   Year
    ------------------------------------------------------------------
    2014-12-15 00:00:00     2015-01-25  23:59:99       12     2014
    2015-01-26 00:00:00     2015-02-25  23:59:99       1      2015
    2015-02-26 00:00:00     2015-03-25  23:59:99       2      2015
    ------------------------------------------------------------------

如何获取结束日期?

无法在“23:59:99”获得 99 秒

试试这个:

CREATE TABLE #temp(StartDate datetime, EndDate datetime, Month int, Year int)

INSERT #temp values
  ('2014-12-15 00:00:00', NULL, 12, 2014),
  ('2015-01-26 00:00:00', NULL, 1, 2015),
  ('2015-02-26 00:00:00', NULL, 2, 2015)

UPDATE t
SET EndDate = DateAdd(m, DateDiff(m, 0, StartDate), '1900-02-25T23:59:59')
FROM #temp t

SELECT * FROM #temp

结果:

StartDate                EndDate                  Month  Year
2014-12-15 00:00:00.000  2015-01-25 23:59:59.000  12     2014
2015-01-26 00:00:00.000  2015-02-25 23:59:59.000  1      2015
2015-02-26 00:00:00.000  2015-03-25 23:59:59.000  2      2015

编辑:

如果您想从下一个开始日期计算结束日期,这里是您的更新语句:

UPDATE t
SET EndDate = (SELECT top 1 DateAdd(s, -1, StartDate) FROM #temp 
               WHERE t.StartDate < StartDate 
               ORDER BY startdate)
FROM #temp t

结果:

StartDate                EndDate                  Month  Year
2014-12-15 00:00:00.000  2015-01-25 23:59:59.000  12     2014
2015-01-26 00:00:00.000  2015-02-25 23:59:59.000  1      2015
2015-02-26 00:00:00.000  null                     2      2015

样本TABLE

CREATE TABLE #TEMP(DATES DATETIME,DATES DATETIME,[MONTH] INT,[YEAR] INT)

INSERT INTO #TEMP
SELECT '2014-12-15 00:00:00',     NULL,       12,      2014
UNION ALL
SELECT '2015-01-26 00:00:00',     NULL,       1,      2015
UNION ALL
SELECT '2015-02-26 00:00:00',     NULL,       2,      2015

查询

;WITH CTE AS
(
    SELECT ROW_NUMBER() OVER(ORDER BY STARTDATE)RNO, STARTDATE
    FROM #TEMP
)
,CTE2 AS
(
    SELECT C1.STARTDATE,
    ISNULL(DATEADD(S,-1,C2.STARTDATE),DATEADD(S,-1,DATEADD(MONTH,1,C1.STARTDATE))) ENDDATE 
    FROM CTE C1
    LEFT JOIN CTE C2 ON C1.RNO=C2.RNO-1
)
UPDATE #TEMP
SET EndDate = CTE2.ENDDATE
FROM #TEMP T1
JOIN CTE2 ON T1.STARTDATE=CTE2.STARTDATE

结果

您也可以使用 OUTER APPLY

select
    c.StartDate
    DATEADD(SECOND,-1,e.StartDate) EndDate
    DATEPART(MONTH,StartDate) [Month],   
    DATEPART(YEAR,StartDate) [Year]
from [Cut-Off] c
outer apply (
    select TOP 1
        e.StartDate
    from [Cut-Off] e
    where
        e.StartDate > c.StartDate
    order by
        e.StartDate
) e