天天看點

trunc與日期

最近在優化一個項目,發現程式員為了取一個日期,它不需要後面的秒,為了實作這個功能,程式員竟然建立一個函數來實作,感覺這樣非常不合理。

我想trunc(sysdate)可以截取日期的年月日,這樣截取到秒應該是支援的,看了以下trunc的文檔,總結如下:

設定環境變量

set NLS_DATE_FORMAT=YYYY-MM-DD HH24:MI:SS

select sysdate x from dual     /*+目前時間 */

union all

select trunc(sysdate) from dual  /*+取年月日 */

select trunc(sysdate,'yy') from dual /*+取年 */

select trunc(sysdate,'mm') from dual  /*+取年月 */

select trunc(sysdate,'dd') from dual  /*+取年月日 同trunc(sysdate)*/

select trunc(sysdate,'hh24') from dual /*+取年月日時 */

select trunc(sysdate,'mi') from dual  /*+取年月日時分 */

select trunc(sysdate,'ww') from dual  

select trunc(sysdate,'w') from dual

select trunc(sysdate,'q') from dual  /* 取目前季度 */

select trunc(sysdate,'d') from dual  /* 取目前星期,從星期日開始 */

X                    

---------------------

2011-05-16 10:7:29   

2011-05-16 00:0:00   

2011-01-01 00:0:00   

2011-05-01 00:0:00   

2011-05-16 10:0:00   

2011-05-16 10:7:00   

2011-05-14 00:0:00   

2011-05-15 00:0:00   

2011-04-01 00:0:00   

11 rows selected.

注:

ww表示Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.      2011/1/1是星期六,是以每個星期從星期六開始。

w表示Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.     2011/5/1是星期日,是以這個月每個星期從星期日開始。

繼續閱讀