时间类型的操作

NUMTODSINTERVAL 可格式化的参数:DAY, HOUR, MINUTE, SECOND
NUMTOYMINTERVAL 可格式化的参数:YEAR, MONTH

select numtoyminterval
       (trunc(months_between(dt2,dt1)),'month')
           years_months,
       numtodsinterval
           (dt2-add_months( dt1, trunc(months_between(dt2,dt1)) ),
            'day' )
           days_hours
  from (select to_date('29-feb-2000 01:02:03','dd-mon-yyyy hh24:mi:ss') dt1,
               to_date('15-mar-2001 11:22:33','dd-mon-yyyy hh24:mi:ss') dt2
          from dual )

当前时间减去7分钟的时间
select sysdate,sysdate - interval '7' MINUTE from dual
当前时间减去7小时的时间
select sysdate - interval '7' hour from dual
当前时间减去7天的时间
select sysdate - interval ’7’ day from dual
当前时间减去7月的时间
select sysdate,sysdate - interval '7' month from dual
当前时间减去7年的时间
select sysdate,sysdate - interval '7' year from dual
时间间隔乘以一个数字
select sysdate,sysdate - 8*interval '7' hour from dual

取整(向下取整):
select floor(5.534) from dual;
select trunc(5.534) from dual;
上面两种用法都可以对数字5.534向下取整,结果为5.


如果要向上取整 ,得到结果为6,则应该用ceil
select ceil(5.534) from dual;

四舍五入:
SELECT round(5.534) FROM dual;
SELECT round(5.534,0) FROM dual;
SELECT round(5.534,1) FROM dual;
SELECT round(5.534,2) FROM dual;
结果分别为 6,  6,  5.5,  5.53

保留N位小数(不四舍五入):
select trunc(5.534,0) from dual;
select trunc(5.534,1) from dual;
select trunc(5.534,2) from dual;
结果分别是 5,5.5,5.53,其中保留0位小数就相当于直接取整了。

你可能感兴趣的:(时间)