Date ranges in T/SQL

前端 未结 4 968
北荒
北荒 2021-01-15 06:44

For a current project I am working I need to return an aggregate report based on date ranges.

I have 3 types of reports, yearly, monthly and daily.

To assis

4条回答
  •  长发绾君心
    2021-01-15 07:06

    A static number table is useful, single column, say 8000 rows FROM 0 TO 7999

    (Not checked)

    DECLARE @Start smalldatetime, @End smalldatetime, @Diff int
    
    SELECT @Start = '2006-01-01 11:10:00', @End = '2009-05-05 08:00:00', @diff = DATEDIFF(year,@start,@end)
    
    SELECT
       DATEADD(year,N.Number,@Start)
    FROM
       dbo.Number N
    WHERE
       N.Number <= @diff
    

提交回复
热议问题