oracle函数trunc()用法

trunc() 截取日期或者数字

参考链接:https://www.cnblogs.com/xiaoyudz/archive/2011/03/18/1988467.html
官方注释:

Syntax
TRUNC(date [, fmt ]) 
Purpose
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day. Please refer to "ROUND and TRUNC Date Functions" for the permitted format models to use in fmt.

Examples
The following example truncates a date:
SELECT TRUNC(TO_DATE('27-OCT-92','DD-MON-YY'), 'YEAR')
  "New Year" FROM DUAL;
 
New Year
---------
01-JAN-92 

日期

-- 2020-01-01
select trunc(TO_DATE('2020-11-08', 'YYYY-MM-DD'),'YYYY') from dual;

数字

-- 默认精度为0,结果为123
select trunc(123.456) from dual;
-- 123.45 采用的是去尾法
select trunc(123.456, 2) from dual;
-- 120
select trunc(123.456, -1) from dual;;
-- 100
select trunc(123.456, -2) from dual;
-- 0
select trunc(123.456, -3) from dual;
posted @ 2022-01-19 17:28  我见青山应如是  阅读(519)  评论(0编辑  收藏  举报