[Oracle SQL Tricks] Generating Dates Between Two Dates


Posted by Vanchy on 2021-02-02

1. Goal

用一段SQL產生指定日期區間內的所有日期

2. Code

SELECT backup_date
FROM ( SELECT trunc(to_date('20210201', 'yyyymmdd') - rownum - 1) as backup_date
       FROM dual
       CONNECT BY rownum <= 5
     )

3.Result

backup_date
2021/02/01
2021/01/31
2021/01/30
2021/01/29
2021/01/28

4. Description

  • to_date(‘20210201’, ‘yyyymmdd’):可換成任何日期
  • rownum <= 5:可調整產生幾筆日期資料

5. Reference

Generate a range of dates using SQL


#Oracle SQL







Related Posts

MTR04_1013

MTR04_1013

[ 筆記 ] 網路基礎 - TCP / IP

[ 筆記 ] 網路基礎 - TCP / IP

API

API


Comments