使用sql preocedure创建日期时间表

时间:2016-06-07 12:17:24

标签: sql sql-server

如何使用过程在Sql server中创建这样的表?

Year   Month   Week  Day  Hour Min
2016     1      1     1    1   1
2016     1      1     1    1   2
-        -      -     -    -   -
2016     1      1     1    1   60
-        -      -     -    -   -
2016     1      1     1    2   60
-        -      -     -    -   -
2016     1      1     1    60  60

2 个答案:

答案 0 :(得分:4)

我使用udf即时创建日期范围。

Select Year=Year(RetVal)
      ,Month=Month(RetVal)
      ,Week=DatePart(WEEK,RetVal)
      ,Day=Day(RetVal)
      ,Hour=DatePart(HH,RetVal)
      ,Min=DatePart(MI,RetVal)
 From [dbo].[udf-Create-Range-Date]('2016-01-01','2016-01-02','MI',1) 

返回

Year    Month   Week    Day Hour    Min
2016    1   1   1   0   0
2016    1   1   1   0   1
2016    1   1   1   0   2
2016    1   1   1   0   3
2016    1   1   1   0   4
2016    1   1   1   0   5
2016    1   1   1   0   6
2016    1   1   1   0   7
2016    1   1   1   0   8
2016    1   1   1   0   9
...

这是函数

CREATE FUNCTION [dbo].[udf-Create-Range-Date] (@DateFrom datetime,@DateTo datetime,@DatePart varchar(10),@Incr int)

Returns 
@ReturnVal Table (RetVal datetime)

As
Begin
    With DateTable As (
        Select DateFrom = @DateFrom
        Union All
        Select Case @DatePart
               When 'YY' then DateAdd(YY, @Incr, df.dateFrom)
               When 'QQ' then DateAdd(QQ, @Incr, df.dateFrom)
               When 'MM' then DateAdd(MM, @Incr, df.dateFrom)
               When 'WK' then DateAdd(WK, @Incr, df.dateFrom)
               When 'DD' then DateAdd(DD, @Incr, df.dateFrom)
               When 'HH' then DateAdd(HH, @Incr, df.dateFrom)
               When 'MI' then DateAdd(MI, @Incr, df.dateFrom)
               When 'SS' then DateAdd(SS, @Incr, df.dateFrom)
               End
        From DateTable DF
        Where DF.DateFrom < @DateTo
    )

    Insert into @ReturnVal(RetVal) Select DateFrom From DateTable option (maxrecursion 32767)

    Return
End

-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','YY',1) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','DD',1) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-31','MI',15) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-02','SS',1) 

答案 1 :(得分:2)

在CTE的帮助下:

CREATE PROCEDURE dbo.dates
    @datestart date,
    @dateend date
AS
BEGIN
    SET NOCOUNT ON;

    WITH cte AS (
    SELECT CAST(@datestart as datetime) as d
    UNION ALL
    SELECT DATEADD(MINUTE,1,d)
    FROM cte
    WHERE d < DATEADD(minute,59,DATEADD(hour,23,CAST(@dateend as datetime)))
    )

    SELECT  DATEPART(YEAR,d) [Year],   
            DATEPART(MONTH,d) [Month],
            DATEPART(WEEK,d) [Week],
            DATEPART(DAY,d) [Day],
            DATEPART(HOUR,d) [Hour],
            DATEPART(MINUTE,d) [Min]
    FROM cte
    OPTION (MAXRECURSION 0)
END

EXEC dbo.dates '2016-01-01', '2016-12-31'

输出:

Year    Month   Week    Day Hour    Min
2016    1       1       1   0       0
2016    1       1       1   0       1
2016    1       1       1   0       2
2016    1       1       1   0       3
2016    1       1       1   0       4
2016    1       1       1   0       5
2016    1       1       1   0       6
2016    1       1       1   0       7
2016    1       1       1   0       8
....
2016    12      53      31  23      59