oracle中的trunc()函数是对时间类型或者数字进行截取操作的。
一般用法为trunc(Date,“fmt”),其中fmt 是我们要展示的时间字段,或者trunc(number),该函数表示取整。
trunc(number)的用法一般有以下几种:
trunc(55.5,-1) = 50;//-1(负数)表示从小数点左边第一位截取后面全置为零;
trunc(55.55,1) = 55.5;//1(正数)表示小数点后面保留一位;
trunc(55.55) = 55;//截取整数部分;默认小数点后保留0位
trunc 函数可用于截取日期时间
用法:trunc(字段名,精度)
具体实例:
在表table1中,有一个字段名为sysdate,该行id=123,日期显示:2016/10/28 15:11:58
trunc(sysdate,'yyyy');//返回当前年的第一天
trunc(sysdate, 'mm');//返回当前月的第一天
trunc(sysdate, 'dd');//返回当前时间的年月日
trunc(sysdate, 'd');//返回当前星期的第一天
trunc(sysdate, 'hh');//返回当前小时
1、截取时间到年时,sql语句如下:
select trunc(sysdate,'yyyy') from table1 where id=123; --yyyy也可用year替换
显示:2016/1/1
2、截取时间到月时,sql语句:
select trunc(sysdate,'mm') from table1 where id=123;
显示:2016/10/1
3、截取时间到日时,sql语句:
select trunc(sysdate,'dd') from table1 where id=123;
显示:2016/10/28
4、截取时间到小时时,sql语句:
select trunc(sysdate,'hh') from table1 where id=123;
显示:2016/10/28 15:00:00
5、截取时间到分钟时,sql语句:
select trunc(sysdate,'mi') from table1 where id=123;
显示:2016/10/28 15:11:00