1. 程式人生 > >trunc函式的用法

trunc函式的用法

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’),會提示“精度說明符過多”