SELECT 2015+level-1 lv FROM DUAL CONNECT BY LEVEL <=2020-2015+1
结果:2015 2016 2017 2018 2019 2020
1、获取日期列表:
SELECT TO_CHAR(TO_DATE('2019-10-01', 'yyyy-MM-dd') + ROWNUM - 1, 'yyyyMMdd') as daylist FROM DUAL CONNECT BY ROWNUM <= trunc(to_date('2019-10-28', 'yyyy-MM-dd') - to_date('2019-10-01', 'yyyy-MM-dd')) + 1
结果: 20191001 20191002 20191003 20191004 20191005 20191006
2、获取月份列表:
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2020-04', 'yyyy-MM'), ROWNUM - 1), 'yyyyMM') as monthlist
FROM DUAL CONNECT BY ROWNUM <= months_between(to_date('2020-10', 'yyyy-MM'), to_date('2020-04', 'yyyy-MM')) + 1
结果:202004 202005 202006 202007 202008 202009 202010
3、获取年份列表:
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2018-10', 'yyyy-MM'), (ROWNUM - 1) * 12), 'yyyy') as yearlist
FROM DUAL CONNECT BY ROWNUM <= months_between(to_date('2020-10', 'yyyy-MM'), to_date('2018-10', 'yyyy-MM')) / 12 + 1
结果:2018 2019 2020
1、增加或减去日期
select sysdate,sysdate + interval '1' year from dual;
结果:2021-07-26 15:59:30 2022-07-26 15:59:30
2、增加或减去年分
select TO_CHAR(SYSDATE, 'yyyy'),TO_CHAR(SYSDATE+ interval '-2' year , 'yyyy') from dual
结果:2021 2020
3、增加或减去月份
-- 利用add_months函数
select to_char(add_months(to_date('1999-12','yyyy-mm'),2),'yyyy-mm') from dual;
结果: 2000-02
select to_char(add_months(to_date('1999-12','yyyy-mm'),-2),'yyyy-mm') from dual;
结果: 1999-10
-- 利用interval时间间隔
select TO_CHAR(SYSDATE, 'yyyy-mm'),TO_CHAR(SYSDATE + interval '-1' month , 'yyyy-mm') from dual
结果:2021-07 2021-06
4、查询当前时间1个月以前的时间:
select sysdate,add_months(sysdate,-1) from dual;
结果:2021-07-26 16:17:00 2021-06-26 16:17:00
5、查询当前时间1个月以后的时间:
select add_months(sysdate,1) from dual;
结果:2021-07-26 16:17:49 2021-08-26 16:17:49
6、返回日期的最后一天
select to_char(last_day(sysdate),'yyyy-mm-dd') from dual;
结果: 2021-07-31
7、返回两个日期之间的月份 date2-date1的月份
select months_between(to_date('2021-10-18','yyyy-mm-dd'),to_date('2021-3-18','yyyy-mm-dd')) from dual;
结果:7
Y或YY或YYY 年的最后一位,两位,三位 SELECTTO_CHAR(SYSDATE,'YYY') FROM TABLE_NAME;
Q 季度,1-3月为第一季度 SELECT TO_CHAR(SYSDATE,'Q') FROM TABLE_NAME;
MM 月份数 SELECT TO_CHAR(SYSDATE,'MM') FROM TABLE_NAME;
RM 月份的罗马表示 SELECT TO_CHAR(SYSDATE,'RM') FROM TABLE_NAME;
month 用9个字符表示的月份名 SELECT TO_CHAR(SYSDATE,'month') FROM TABLE_NAME;
ww 当年第几周 SELECT TO_CHAR(SYSDATE,'ww') FROM TABLE_NAME;
w 本月第几周 SELECT TO_CHAR(SYSDATE,'w') FROM TABLE_NAME;
DDD 当年第几天,一月一日为001 ,二月一日032 SELECT TO_CHAR(SYSDATE,'DDD') FROM TABLE_NAME;
DD 当月第几天 SELECT TO_CHAR(SYSDATE,'DD') FROM TABLE_NAME;
D 周内第几天 SELECT TO_CHAR(SYSDATE,'D') FROM TABLE_NAME; 如 sunday
DY 周内第几天缩写 SELECT TO_CHAR(SYSDATE,'DY') FROM TABLE_NAME; 如 sun
hh12 12小时制小时数 SELECT TO_CHAR(SYSDATE,'hh12') FROM TABLE_NAME;
hh24 24小时制小时数 SELECT TO_CHAR(SYSDATE,'hh24') FROM TABLE_NAME;
Mi 分钟数 SELECT TO_CHAR(SYSDATE,'Mi') FROM TABLE_NAME;
ss 秒数 SELECT TO_CHAR(SYSDATE,'ss') FROM TABLE_NAME;
TO_NUMBER() 将合法的字符串转换为数字 SELECT TO_NUMBER ('88877') FROM TABLE_NAME; 88877
TO_CHAR() 将数字转换为字符串 SELECT TO_CHAR(88877) FROM TABLE_NAME; '88877'