Oracle根據當前時間獲取最近5年,最近6個月,最近30天的時間
根據當前時間獲取最近5年時間:(yyyy)
SELECT to_char(SYSDATE,'YYYY') - LEVEL + 1 as "xAxisValue"
FROM dualCONNECT BY LEVEL <= 5
ORDER BY to_char(SYSDATE,'YYYY') - LEVEL + 1 ASC
根據當前時間獲取最近6個月時間(yyyy-mm):
SELECT
TO_CHAR(add_months(to_date(to_char(trunc(sysdate, 'mm')+1, 'yyyy-mm'), 'yyyy-mm'), -(ROWNUM - 1)), 'yyyy-mm') as "xAxisValue"
FROM dual
CONNECT BY ROWNUM <= 6
ORDER BY TO_CHAR(add_months(to_date(to_char(trunc(sysdate, 'mm')+1, 'yyyy-mm'), 'yyyy-mm'), -(ROWNUM - 1)), 'yyyy-mm') ASC
根據當前時間獲取最近30天時間:
SELECT to_char(day,'yyyy-mm-dd') as "xAxisValue"
from (
SELECT to_date(to_char(trunc(sysdate, 'dd'), 'yyyy-mm-dd'), 'yyyy-mm-dd') - rownum + 1 as day
FROM dual
CONNECT BY ROWNUM <= 30
)
ORDER BY day asc
也可根據輸入起止時間來獲取時間段內的天:如下為獲取2017-06-10到2017-07-10時間段內的天(yyyy-mm-dd)
SELECT to_char(day,'yyyy-mm-dd') as "xAxisValue"
from (
SELECT to_date(to_char(trunc(to_date('2017-07-10', 'yyyy-mm-dd'), 'dd'), 'yyyy-mm-dd'), 'yyyy-mm-dd') - rownum + 1 as day
FROM dual
CONNECT BY ROWNUM <= (to_date('2017-07-10', 'yyyy-mm-dd') - to_date( '2017-06-10', 'yyyy-mm-dd') + 1)
)
ORDER BY day asc