1.
select last_day(to_date('20230101','YYYYMMDD')) from dual;
select last_day(to_date(V_END_DATE,'YYYYMMDD')) from dual; --查询任意一天 当月的最后一天
2.
select to_char(to_date('20230101','YYYYMMDD')-1,'YYYYMMDD') INTO V_START_DATE FROM DUAL;
select to_char(to_date(V_END_DATE,'YYYYMMDD')-1,'YYYYMMDD') INTO V_START_DATE FROM DUAL; --查询任意一天的前一天
--字符型转日期型 -1 之后再转字符型
3.
select to_char(last_day(add_months(SYSDATE, -2)) + 1,'yyyymmdd') First_DayOfMonth from dual; --查询上一个月第一天
select to_char(last_day(add_months(SYSDATE, -1)),'yyyymmdd') Last_DayOfMonth from dual; --查询上一个月最后一天
4.
select to_number(add_months(trunc(to_date(V_FIRST_DAY,'yyyymmdd'), 'mm'), 1) - trunc(to_date(V_FIRST_DAY,'yyyymmdd'), 'mm')) into from dual; --指定月份天数,V_FIRST_DAY为那个月份任意一天
5.
查询指定月份/年份/日期的第一天和最后一天
SELECT TO_CHAR(TRUNC(TO_DATE('2018-06','YYYY-MM'), 'MM'), 'YYYY-MM-DD') "指定日期第一天" FROM DUAL;
SELECT TO_CHAR(LAST_DAY(TO_DATE('2018-06','YYYY-MM')), 'YYYY-MM-DD') "指定日期最后一天" FROM DUAL;
--年
select trunc(sysdate, 'yy') "当年第一天" from dual;
select trunc(sysdate, 'yyyy') "当年第一天" from dual;
select trunc(sysdate, 'year') "当年第一天" from dual;
--季度
select trunc(sysdate, 'q') "当前季度的第一天" from dual;
select trunc(add_months(sysdate,3), 'Q') -1/24 "这个季度最后一天23点" from dual;
--月
select trunc(sysdate, 'mm') "当月第一天" from dual;
select trunc(sysdate, 'month') "当月第一天" from dual;
select trunc(last_day(sysdate)+1) "下个月第一天的0点" from dual;
--周
select trunc(sysdate, 'd') "本周的第一天,周日起" from dual;
select trunc(sysdate,'day') "本周的第一天,周日起" from dual;
select next_day(TRUNC(SYSDATE ), 'TUESDAY' ) + 12/24 "下个星期二中午12点" from dual;
select TRUNC(LEAST(NEXT_DAY(SYSDATE, 'SATURDAY'), NEXT_DAY(SYSDATE, 'SUNDAY'))) + (6*60+10)/(24*60) "下个周六日早上6点10分" from dual;
--天
select trunc(sysdate+1) "今晚12点" from dual;
select trunc(sysdate+1) + (8*60+30)/(24*60) "明天早上8点30分" from dual;
--时
select trunc(sysdate, 'hh')"当前小时" from dual;
select trunc(sysdate, 'hh24') "当前小时" from dual;
--分
select trunc(sysdate, 'mi') "当前分钟" from dual;
select trunc(sysdate,'mi') + 10/ (24*60) "10分钟后的时间"from dual;
--秒
select sysdate + 10/(24 * 60 * 60) "10秒钟后" from dual;--trunc没有精确到秒的精度