Generate a range of dates using SQL

前端 未结 15 1300
面向向阳花
面向向阳花 2020-11-28 05:47

I have a SQL query that takes a date parameter (if I were to throw it into a function) and I need to run it on every day of the last year.

How to generate a list of

相关标签:
15条回答
  • 2020-11-28 05:56

    I had the same requirement - I just use this. User enters the number of days by which he/she wants to limit the calendar range to.

      SELECT DAY, offset
        FROM (SELECT to_char(SYSDATE, 'DD-MON-YYYY') AS DAY, 0 AS offset
                FROM DUAL
              UNION ALL
              SELECT to_char(SYSDATE - rownum, 'DD-MON-YYYY'), rownum
                FROM all_objects d)
                where offset <= &No_of_days
    

    I use the above result set as driving view in LEFT OUTER JOIN with other views involving tables which have dates.

    0 讨论(0)
  • 2020-11-28 05:59

    There's no need to use extra large tables or ALL_OBJECTS table:

    SELECT TRUNC (SYSDATE - ROWNUM) dt
      FROM DUAL CONNECT BY ROWNUM < 366
    

    will do the trick.

    0 讨论(0)
  • 2020-11-28 05:59

    A method quite frequently used in Oracle is something like this:

    select trunc(sysdate)-rn
    from
    (   select rownum rn
        from   dual
        connect by level <= 365)
    /
    

    Personally, if an application has a need for a list of dates then I'd just create a table with them, or create a table with a series of integers up to something ridiculous like one million that can be used for this sort of thing.

    0 讨论(0)
  • 2020-11-28 05:59

    Better late than never. Here's a method that I devised (after reading this post) for returning a list of dates that includes: (a) day 1 of of the current month through today, PLUS (b) all dates for the past two months:

    select (sysdate +1 - rownum) dt 
    from dual 
     connect by rownum <= (sysdate - add_months(sysdate - extract(day from sysdate),-2));
    

    The "-2" is the number of prior full months of dates to include. For example, on July 10th, this SQL returns a list of all dates from May 1 through July 10 - i.e. two full prior months plus the current partial month.

    0 讨论(0)
  • 2020-11-28 06:01

    Recently I had a similar problem and solved it with this easy query:

    SELECT
      (to_date(:p_to_date,'DD-MM-YYYY') - level + 1) AS day
    FROM
      dual
    CONNECT BY LEVEL <= (to_date(:p_to_date,'DD-MM-YYYY') - to_date(:p_from_date,'DD-MM-YYYY') + 1);
    

    Example

    SELECT
      (to_date('01-05-2015','DD-MM-YYYY') - level + 1) AS day
    FROM
      dual
    CONNECT BY LEVEL <= (to_date('01-05-2015','DD-MM-YYYY') - to_date('01-04-2015','DD-MM-YYYY') + 1);
    

    Result

    01-05-2015 00:00:00
    30-04-2015 00:00:00
    29-04-2015 00:00:00
    28-04-2015 00:00:00
    27-04-2015 00:00:00
    26-04-2015 00:00:00
    25-04-2015 00:00:00
    24-04-2015 00:00:00
    23-04-2015 00:00:00
    22-04-2015 00:00:00
    21-04-2015 00:00:00
    20-04-2015 00:00:00
    19-04-2015 00:00:00
    18-04-2015 00:00:00
    17-04-2015 00:00:00
    16-04-2015 00:00:00
    15-04-2015 00:00:00
    14-04-2015 00:00:00
    13-04-2015 00:00:00
    12-04-2015 00:00:00
    11-04-2015 00:00:00
    10-04-2015 00:00:00
    09-04-2015 00:00:00
    08-04-2015 00:00:00
    07-04-2015 00:00:00
    06-04-2015 00:00:00
    05-04-2015 00:00:00
    04-04-2015 00:00:00
    03-04-2015 00:00:00
    02-04-2015 00:00:00
    01-04-2015 00:00:00
    
    0 讨论(0)
  • 2020-11-28 06:01

    This query generates a list of dates 4000 days in the future and 5000 in the past as of today (inspired on http://blogs.x2line.com/al/articles/207.aspx):

    SELECT * FROM (SELECT
        (CONVERT(SMALLDATETIME, CONVERT(CHAR,GETDATE() ,103)) + 4000 -
                    n4.num * 1000 -
                    n3.num * 100 -
                    n2.num * 10 -
                    n1.num) AS Date, 
        year(CONVERT(SMALLDATETIME, CONVERT(CHAR,GETDATE() ,103)) + 4000 -
                    n4.num * 1000 -
                    n3.num * 100 -
                    n2.num * 10 -
                    n1.num) as Year,
        month(CONVERT(SMALLDATETIME, CONVERT(CHAR,GETDATE() ,103)) + 4000 -
                    n4.num * 1000 -
                    n3.num * 100 -
                    n2.num * 10 -
                    n1.num) as Month,
        day(CONVERT(SMALLDATETIME, CONVERT(CHAR,GETDATE() ,103)) + 4000 -
                    n4.num * 1000 -
                    n3.num * 100 -
                    n2.num * 10 -
                    n1.num) as Day
               FROM (SELECT 0 AS num union ALL
                     SELECT 1 UNION ALL
                     SELECT 2 UNION ALL
                     SELECT 3 UNION ALL
                     SELECT 4 UNION ALL
                     SELECT 5 UNION ALL
                     SELECT 6 UNION ALL
                     SELECT 7 UNION ALL
                     SELECT 8 UNION ALL
                     SELECT 9) n1
                   ,(SELECT 0 AS num UNION ALL
                     SELECT 1 UNION ALL
                     SELECT 2 UNION ALL
                     SELECT 3 UNION ALL
                     SELECT 4 UNION ALL
                     SELECT 5 UNION ALL
                     SELECT 6 UNION ALL
                     SELECT 7 UNION ALL
                     SELECT 8 UNION ALL
                     SELECT 9) n2
                   ,(SELECT 0 AS num union ALL
                     SELECT 1 UNION ALL
                     SELECT 2 UNION ALL
                     SELECT 3 UNION ALL
                     SELECT 4 UNION ALL
                     SELECT 5 UNION ALL
                     SELECT 6 UNION ALL
                     SELECT 7 UNION ALL
                     SELECT 8 UNION ALL
                     SELECT 9) n3  
                   ,(SELECT 0 AS num UNION ALL
                     SELECT 1 UNION ALL
                     SELECT 2 UNION ALL
                     SELECT 3 UNION ALL
                     SELECT 4 UNION ALL
                     SELECT 5 UNION ALL
                     SELECT 6 UNION ALL
                     SELECT 7 UNION ALL
                     SELECT 8) n4
            ) GenCalendar  ORDER BY 1
    
    0 讨论(0)
提交回复
热议问题