trunc 函數可用於截取日期時間
用法:trunc(字段名,精度)
具體實例:
在表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'),會提示“精度說明符過多”