函數TRUNC返回一個根據指定模板模式截斷的日期類型值。
如果沒有使用模板模式,那么就把日期截斷到離它最近的一天。下面的表格顯示了函數TRUNC所允許使用的模板模式。
Pattern | Description |
---|---|
CC, SCC | Returns January 1, cc01 where cc is first 2 digits of the given year |
SYYY, YYYY, YEAR, SYEAR, YYY, YY, Y | Returns January 1, yyyy where yyyy is the given year |
IYYY, IYY, IY, I | Returns the start date of the ISO year containing the given date |
Q | Returns the first day of the quarter containing the given date |
MONTH, MON, MM, RM | Returns the first day of the specified month |
WW | Returns the largest date just prior to, or the same as the given date that corresponds to the same day of the week as the first day of the year |
IW | Returns the start of the ISO week containing the given date |
W | Returns the largest date just prior to, or the same as the given date that corresponds to the same day of the week as the first day of the month |
DDD, DD, J | Returns the start of the day for the given date |
DAY, DY, D | Returns the start of the week (Sunday) containing the given date |
HH, HH12, HH2 4 | Returns the start of the hour |
MI | Returns the start of the minute |
TRUNC函數示例
下面的示例把日期向前截斷為日期所在世紀的開始日期。
SELECT TO_CHAR(TRUNC(TO_DATE('1951','YYYY'),'CC'),'DD-MON-YYYY') "Century" FROM DUAL;
Century
-------------
01-JAN-1901
(1 row)
下面的示例把日期截斷到日期中年份的開始日期。
SELECT TO_CHAR(TRUNC(TO_DATE('01-JUL-1999','DD-MON-YYYY'),'Y'),'DD-MON-YYYY') "Year" FROM DUAL;
Year
-------------
01-JAN-1999
(1 row)
下面的示例把日期截斷到ISO年份的開始日期。
SELECT TO_CHAR(TRUNC(TO_DATE('01-JUL-2004','DD-MON-YYYY'),'IYYY'),'DD-MON-YYYY') "ISO Year" FROM DUAL;
ISO Year
-------------
29-DEC-2003
(1 row)
下面的示例把日期截斷到當前日期所在季度的開始日期。
SELECT TRUNC(TO_DATE('16-FEB-07','DD-MON-YY'),'Q') "Quarter" FROM DUAL;
Quarter
--------------------
01-JAN-07 00:00:00
(1 row)
下面的示例把日期截斷到一個月的開始日期。
SELECT TRUNC(TO_DATE('16-DEC-07','DD-MON-YY'),'MONTH') "Month" FROM DUAL;
Month
--------------------
01-DEC-07 00:00:00
(1 row)
下面的示例把日期截斷到日期所在周的開始日期,周的開始日期是由一年中第一天的周日期決定的。例如:2007年的第一天是周一,所以在1月19日前面的周一的日期是1月15號。
SELECT TRUNC(TO_DATE('19-JAN-07','DD-MON-YY'),'WW') "Week" FROM DUAL;
Week
--------------------
15-JAN-07 00:00:00
(1 row)
下面的示例將日期截斷到一個ISO周的開始。一個ISO周在星期一開始,落在ISO周的日期是2004年1月2號,那么這個ISO周從2003年12月29日開始。
SELECT TRUNC(TO_DATE('02-JAN-04','DD-MON-YY'),'IW') "ISO Week" FROM DUAL;
ISO Week
--------------------
29-DEC-03 00:00:00
(1 row)
下面的示例把日期截斷到一周的開始日期,周的開始日期被認為和一個月的第一天相同。
SELECT TRUNC(TO_DATE('21-MAR-07','DD-MON-YY'),'W') "Week" FROM DUAL;
Week
--------------------
15-MAR-07 00:00:00
(1 row)
下面的示例把日期截斷到一天的開始時間。
SELECT TRUNC(TO_DATE('04-AUG-07 12:00:00 PM','DD-MON-YY HH:MI:SS AM'),'J') "Day" FROM DUAL;
Day
--------------------
04-AUG-07 00:00:00
(1 row)
下面的示例把日期截斷到一周的開始日期(周日)。
SELECT TRUNC(TO_DATE('09-AUG-07','DD-MON-YY'),'DAY') "Day of Week" FROM DUAL;
Day of Week
--------------------
05-AUG-07 00:00:00
(1 row)
下面的示例把日期截斷到小時。
SELECT TO_CHAR(TRUNC(TO_DATE('09-AUG-07 08:30','DD-MON-YY HH:MI'),'HH'),'DD-MON-YY HH24:MI:SS') "Hour" FROM DUAL;
Hour
--------------------
09-AUG-07 08:00:00
(1 row)
下面的示例把日期截斷到分鐘。
SELECT TO_CHAR(TRUNC(TO_DATE('09-AUG-07 08:30:30','DD-MON-YY HH:MI:SS'),'MI'),'DD-MON-YY HH24:MI:SS') "Minute" FROM DUAL;
Minute
--------------------
09-AUG-07 08:30:00
(1 row)