Oracle获取某个日期--日期校验

--获取周六周天

--获取周末

SELECT seq_a_baz002.nextval FROM dual;

--INSERT INTO zf02

SELECT seq_a_baz001.nextval,seq_a_baz002.currval, to_char(T.my_DAY,'yyyymmdd'), '1' FROM (

SELECT TO_DATE('2023-01-01','yyyy-mm-dd')+ROWNUM-1 my_DAY FROM dual CONNECT BY

rownum

) T

WHERE to_char(my_DAY,'day') IN ('星期六', '星期日','saturday', 'sunday');

;

Oracle获取一周前,一个月前,一年前的日期,一个月的总天数、一个月的全部日期

1、获取10分钟前的日期

select sysdate,sysdate-interval '10' minute from dual;

2、获取一周前的日期

select sysdate, sysdate - interval '7' day from dual;

3、获取一个月前的日期

select sysdate,sysdate-interval '1' month from dual;

4、获取一年前的日期

select sysdate,sysdate-interval '1' year from dual;

5、获取当月的总天数

select to_number(to_char(last_day(sysdate),'dd')) from dual;

6、获取某一个月的总天数

select to_number(to_char(last_day(to_date('2018-09','yyyy-mm')),'dd')) from dual;

7、查询某一个月的全部日期

SELECT TO_CHAR(TRUNC(to_date('2018-09','yyyy-MM'), 'MM') + ROWNUM - 1,'yyyy-MM-dd') someday FROM DUAL

CONNECT BY ROWNUM

--批量生成月份不包含最小日期 ROWNUM-0

SELECT TO_CHAR(ADD_MONTHS(TO_DATE('201811','YYYYMM'),ROWNUM-0 ),'YYYYMM') DAY_ID

FROM DUAL CONNECT BY ROWNUM

--批量生成月份

SELECT TO_CHAR(ADD_MONTHS(TO_DATE('201811','YYYYMM'),ROWNUM-1 ),'YYYYMM') DAY_ID

FROM DUAL CONNECT BY ROWNUM

--批量生成年份

SELECT 2007 + ROWNUM || '12' 月份 FROM dual CONNECT BY ROWNUM < 16;

--转换成正常日期格式(yyyy-mm-dd hh24:mi:ss)--

select to_char(cast(cast('26-9月 -17 06.46.00.000000000 下午' as timestamp) as date),'yyyy-mm-dd hh24:mi:ss') dateStr from dual;

SELECT to_char(cast(cast(alc034 as timestamp) as date),'yyyy-mm-dd hh24:mi:ss') FROM ZGGS08;

--校验日期合法

CREATE OR REPLACE FUNCTION is_date(parameter VARCHAR2) RETURN NUMBER IS

  val DATE;

BEGIN

  val := TO_DATE(NVL(parameter, 'a'), 'yyyy-mm-dd');

  RETURN 1;

EXCEPTION

  WHEN OTHERS THEN

    RETURN 0;

END;

select is_date('20181201') from dual;

你可能感兴趣的:(oracle,数据库,sql)