TRUNC函数:用于截断时间或数值,返回指定的值
一、截取时间
select TRUNC(sysdate,'yyyy') from dual; -- 返回当年的第一天 2023/01/01
select TRUNC(sysdate,'mm') from dual; -- 返回当月的第一天 2023/06/01
select TRUNC(sysdate,'dd') from dual; -- 返回当前的年月日 2023/06/12
select TRUNC(sysdate,'d') from dual; -- 返回当前星期的第一天(周日) 2023/6/11
select TRUNC(sysdate,'hh') from dual; -- 返回当前日期,截取到小时,分秒补0 2023/6/12 9:00:00
select TRUNC(sysdate,'mi') from dual; -- 返回当前日期,截取到分钟,秒补0 2023/6/12 9:50:00
二、截取数值
语法:TRUNC(number[,decimals]) 注意这里的截断均不做四舍五入
number:表示要截取的数字
decimals:表示截取位数,[]圈中表示可选参数,注意,若为负数则表示从小数点左边开始截取
select TRUNC(98765.4321) from dual; -- 默认取整 98765
select TRUNC(98765.4321,2) from dual; -- 截取2位小数 98765.43
select TRUNC(98765.4321,-2) from dual; -- 从小数点左边开始截取两位 98700