简体   繁体   中英

How to generate series for date range with minutes interval in oracle?

In Postgres below query is working using generate_series function

SELECT dates 
FROM generate_series(CAST('2019-03-01' as TIMESTAMP), CAST('2019-04-01' as TIMESTAMP),  interval '30 mins') AS dates

Below query is also working in Oracle but only for date interval

select to_date('2019-03-01','YYYY-MM-DD') + rownum -1 as dates 
from all_objects 
where rownum <= to_date('2019-03-06','YYYY-MM-DD')-to_date('2019-03-01','YYYY-MM-DD')+1

SELECT dates 
FROM generate_series(CAST('2019-03-01' as TIMESTAMP), CAST('2019-04-01' as TIMESTAMP),  interval '30 mins') AS dates

I want same result in Oracle for below query

SELECT dates 
FROM generate_series(CAST('2019-03-01' as TIMESTAMP), CAST('2019-04-01' as TIMESTAMP),  interval '30 mins') AS dates

Use a hierarchical query:

SELECT DATE '2019-03-01' + ( LEVEL - 1 ) * INTERVAL '30' MINUTE AS dates
FROM   DUAL
CONNECT BY DATE '2019-03-01' + ( LEVEL - 1 ) * INTERVAL '30' MINUTE <= DATE '2019-04-01';

Output :

\n|  DATES | \n|  :------------------ | \n|  2019-03-01 00:00:00 | \n|  2019-03-01 00:30:00 | \n|  2019-03-01 01:00:00 | \n|  2019-03-01 01:30:00 | \n|  2019-03-01 02:00:00 | \n|  2019-03-01 02:30:00 | \n|  2019-03-01 03:00:00 | \n|  2019-03-01 03:30:00 | \n|  2019-03-01 04:00:00 | \n|  2019-03-01 04:30:00 | \n|  2019-03-01 05:00:00 | \n|  2019-03-01 05:30:00 | \n... \n|  2019-03-31 19:30:00 | \n|  2019-03-31 20:00:00 | \n|  2019-03-31 20:30:00 | \n|  2019-03-31 21:00:00 | \n|  2019-03-31 21:30:00 | \n|  2019-03-31 22:00:00 | \n|  2019-03-31 22:30:00 | \n|  2019-03-31 23:00:00 | \n|  2019-03-31 23:30:00 | \n|  2019-04-01 00:00:00 | \n

db<>fiddle here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM