Oracle-trunc函数的⽤法
trunc 函数可⽤于截取⽇期时间
⽤法:trunc(字段名,精度)
具体实例:
-- 按1分钟聚合
select trunc(stime, 'MI') as stime
-- 按1⼩时聚合
select trunc(stime, 'HH') as stime
-- 按1天聚合
select trunc(stime, 'DD') as stime
-- ⽰例truncate多张表加逗号吗
select trunc(cast('2017-11-09 17:42:57'as timestamp), 'MI') as stime
select trunc('2017-11-09 17:42:57', 'MI') as stime  两个查询语句数据结果⼀样
--返回结果
2017-11-09 17:42:00
-- 按5分钟聚合
trunc(minutes_sub(stime, minute(stime) % 5), 'MI')
-- 按10分钟聚合
trunc(minutes_sub(stime, minute(stime) % 10), 'MI')
-- ⽰例
select trunc(minutes_sub('2017-11-09 17:46:57', minute('2017-11-09 17:46:57') % 5), 'MI')
--返回结果
2017-11-09 17:45:00
在表table1中,有⼀个字段名为sysdate,该⾏id=123,⽇期显⽰:2016/10/28 15:11:58
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
6、截取时间到秒暂时不知道怎么操作
7、不可直接⽤trunc(sysdate,'yyyy-mm-dd'),会提⽰“精度说明符过多”

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。