Calendar table in SQL

后端 未结 3 401
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 05:59

How to create a table that contains a single column of date data type, that consists of all days starting from Jan 1, 2000 until today in oracle SQL

相关标签:
3条回答
  • 2021-01-28 06:24

    As your question is not clear, I'm assuming you want like this.

    SQL server Platform.

    CREATE table mycalender
    (
    mydate date
    );
    
    declare @startdate as date = '2000-01-01'
    
    WHILE(@startdate!=DATEADD(D, 0, DATEDIFF(D, 0, GETDATE())))
    begin
    insert into mycalender VALUES(@startdate)
    set @startdate=dateadd(day,1,@startdate)
    end
    
    0 讨论(0)
  • 2021-01-28 06:36

    You can use a Oracle row generator:

     insert into your_table ( your_column )
     select 
        to_date('2000/01/01', 'yyyy/mm/dd') + N.n
     from
      (SELECT ROWNUM n
       FROM   ( SELECT 1 just_a_column
             FROM   dual
             CONNECT BY LEVEL <= 
                   SYSDATE
                   - to_date('2000/01/01', 'yyyy/mm/dd') 
                   + 1
               ) T
      ) N
      where
          to_date('2000/01/01', 'yyyy/mm/dd') + N.n <= SYSDATE
    
    0 讨论(0)
  • 2021-01-28 06:37

    If this is MS SQL Server try this:

    DECLARE @DateFrom DATETIME = '20000101';
    DECLARE @DateTo DATETIME = GETDATE();
    
    WITH Dates(date)
    AS
    ( 
    SELECT @DateFrom 
    UNION ALL
    SELECT DATEADD(day,1,Dates.date) FROM Dates WHERE Dates.date < DATEADD(DAY, -1, @DateTo)
    )
    
    SELECT * FROM Dates OPTION (MAXRECURSION 5000);
    
    0 讨论(0)
提交回复
热议问题