How can I generate a temporary table filled with dates in SQL Server 2000?

后端 未结 11 522
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 05:21

I need to make a temporary table that holds of range of dates, as well as a couple of columns that hold placeholder values (0) for future use. The dates I need are the firs

相关标签:
11条回答
  • 2020-11-30 05:53

    This will quickly populate a table with 170 years worth of dates.

    CREATE TABLE CalendarMonths (
      date DATETIME,
      PRIMARY KEY (date)
    )
    
    DECLARE
      @basedate DATETIME,
      @offset   INT
    SELECT
      @basedate = '01 Jan 2000',
      @offset = 1
    
    WHILE (@offset < 2048)
    BEGIN
      INSERT INTO CalendarMonths SELECT DATEADD(MONTH, @offset, date) FROM CalendarMonths
      SELECT @offset = @offset + @offset
    END
    

    You can then use it by LEFT joining on to that table, for the range of dates you require.

    0 讨论(0)
  • 2020-11-30 05:54

    Tested below and it works, though it's a bit convoluted.

    I assigned arbitrary values to the dates for the test.

    DECLARE @SD smalldatetime,
            @ED smalldatetime,
            @FD smalldatetime,
            @LD smalldatetime,
            @Mct int,
            @currct int = 0
    
    SET @SD = '1/15/2011'
    SET @ED = '2/02/2012'
    
    
    SET @FD = (DATEADD(dd, -1*(Datepart(dd, @SD)-1), @sd))
    SET @LD = (DATEADD(dd, -1*(Datepart(dd, @ED)-1), @ED))
    
    SET @Mct = DATEDIFF(mm, @FD, @LD)
    
    CREATE TABLE #MyTempTable (FoM smalldatetime, Trials int, Sales money)
    
    WHILE @currct <= @Mct
    BEGIN
        INSERT INTO #MyTempTable (FoM, Trials, Sales)
        VALUES
        (DATEADD(MM, @currct, @FD), 0, 0)
        SET @currct = @currct + 1
    END
    
    
    SELECT * FROM #MyTempTable
    
    DROP TABLE #MyTempTable
    
    0 讨论(0)
  • 2020-11-30 05:56

    I needed something similar, but all DAYS instead of all MONTHS.

    Using the code from MatBailie as a starting point, here's the SQL for creating a permanent table with all dates from 2000-01-01 to 2099-12-31:

    CREATE TABLE _Dates (
      d DATE,
      PRIMARY KEY (d)
    )
    DECLARE @dIncr DATE = '2000-01-01'
    DECLARE @dEnd DATE = '2100-01-01'
    
    WHILE ( @dIncr < @dEnd )
    BEGIN
      INSERT INTO _Dates (d) VALUES( @dIncr )
      SELECT @dIncr = DATEADD(DAY, 1, @dIncr )
    END
    
    0 讨论(0)
  • 2020-11-30 05:57
    SELECT  P.Id
          , DATEADD ( DD, -P.Id, P.Date ) AS Date
        FROM  (SELECT TOP 1000 ROW_NUMBER () OVER (ORDER BY (SELECT NULL)) AS Id, CAST(GETDATE () AS DATE) AS Date FROM master.dbo.spt_values) AS P
    

    This query returns a table calendar for the last 1000 days or so. It can be put in a temporary or other table.

    0 讨论(0)
  • 2020-11-30 06:00

    Create a table variable containing a date for each month in a year:

    declare @months table (reportMonth date, PRIMARY KEY (reportMonth));
    declare @start date = '2018', @month int = 0; -- base 0 month
    while (@offset < 12)
    begin
        insert into @reportMonths select dateAdd(month, @offset, @start);
        select @offset = @offset + 1; 
    end
    
    0 讨论(0)
提交回复
热议问题