oracle数据库中日期加减函数

1.oracle中当前系统时间加减用法

select add_months(sysdate, 12) "NEW YEAR" from dual; /**系统时间+12个月后的时间**/
select extract(month from sysdate) "This Month" from dual; /**获取系统时间所在月份**/
select (sysdate + 1) "NEW DATE" from dual; /**系统时间+1天后的时间**/

 
2.下面一日期处理函数为网上看到一并写在这里

create or replace function DATEADD(datepart varchar2,
                                   num      number,
                                   indate   date) return date is
  Result     date;
  v_sql      varchar2(1000);
  v_datepart varchar2(30);
  v_ms       varchar2(13);
begin
  v_datepart := lower(datepart);
  /*
     Datepart         Abbreviations 
    year             yy, y
     quarter          qq, q
     month            mm, m
     day              dd, d
     week             wk, w
     hour             hh, h
     minute           mi, n
     second           ss, s
     millisecond      ms
  */
  case
    when v_datepart in ('year', 'yy', 'y') then
      v_sql := 'select :1 + interval ''' || num || ''' year from dual';
    when v_datepart in ('quarter', 'qq', 'q') then
      v_sql := 'select :1 + (interval ''3'' month) * ' || num ||
               ' from dual';
    when v_datepart in ('month', 'mm', 'm') then
      v_sql := 'select :1 + interval ''' || num || ''' month from dual';
    when v_datepart in ('week', 'wk', 'w') then
      v_sql := 'select :1 + (interval ''7'' day) * ' || num || ' from dual';
    when v_datepart in ('day', 'dd', 'd') then
      v_sql := 'select :1 + interval ''' || num || ''' day from dual';
    when v_datepart in ('hour', 'hh') then
      v_sql := 'select :1 + interval ''' || num || ''' hour from dual';
    when v_datepart in ('minute', 'mi', 'n') then
      v_sql := 'select :1 + interval ''' || num || ''' minute from dual';
    when v_datepart in ('second', 'ss', 's') then
      v_sql := 'select :1 + interval ''' || num || ''' second from dual';
    when v_datepart in ('millisecond', 'ms') then
      v_ms  := to_char(num / 1000, 'fm999999990.000');
      v_sql := 'select :1 + interval ''' || v_ms ||
               ''' second(9,3) from dual';
    else
      RAISE_APPLICATION_ERROR(-20001,
                              '''' || datepart ||
                              ''' is not a recognized dateadd option.');
  end case;

  execute immediate v_sql
    into Result
    using indate;

  return(Result);

EXCEPTION
  WHEN OTHERS THEN
    RAISE;
  
end DATEADD;

 

 

你可能感兴趣的:(oracle)