|
Format Mask |
Rounds or Truncates to |
|---|---|
|
CC or SSC |
Century |
|
SYYY, YYYY, YEAR, SYEAR, YYY, YY, or Y |
Year (rounds up to next year on July 1) |
|
IYYY, IYY, IY, or I |
Standard ISO year |
|
Q |
Quarter (rounds up on the sixteenth day of the second month of the quarter) |
|
MONTH, MON, MM, or RM |
Month (rounds up on the sixteenth day, which is not necessarily the same as the middle of the month) |
|
WW |
Same day of the week as the first day of the year |
|
IW |
Same day of the week as the first day of the ISO year |
|
W |
Same day of the week as the first day of the month |
|
DDD, DD, or J |
Day |
|
DAY, DY, or D |
Starting day of the week |
|
HH, HH12, HH24 |
Hour |
|
MI |
Minute |
Example:
Round up to the next century:
TO_CHAR (ROUND (TO_DATE ('01-MAR-1994'), 'CC'), 'DD-MON-YYYY')
==> 01-JAN-2000
Round back to the beginning of the current century:
TO_CHAR (ROUND (TO_DATE ('01-MAR-1945'), 'CC'), 'DD-MON-YYYY')
==> 01-JAN-1900
Round down and up to the first of the year:
ROUND (TO_DATE ('01-MAR-1994'), 'YYYY') ==> 01-JAN-1994
ROUND (TO_DATE ('01-SEP-1994'), 'YEAR') ==> 01-JAN-1995
Round up and down to the quarter (first date in the quarter):
ROUND (TO_DATE ('01-MAR-1994'), 'Q') ==> 01-APR-1994
ROUND (TO_DATE ('15-APR-1994'), 'Q') ==> 01-APR-1994
Round down and up to the first of the month:
ROUND (TO_DATE ('12-MAR-1994'), 'MONTH') ==> 01-MAR-1994
ROUND (TO_DATE ('17-MAR-1994'), 'MM') ==> 01-APR-1994
Day of first of year is Saturday:
TO_CHAR (TO_DATE ('01-JAN-1994'), 'DAY') ==> 'SATURDAY'
So round to date of nearest Saturday for `01-MAR-1994':
ROUND (TO_DATE ('01-MAR-1994'), 'WW') ==> 26-FEB-1994
First day in the month is a Friday:
TO_CHAR (TO_DATE ('01-APR-1994'), 'DAY') ==> FRIDAY
So round to date of nearest Friday from April 16, 1994:
TO_CHAR ('16-APR-1994'), 'DAY') ==> SATURDAY
ROUND (TO_DATE ('16-APR-1994'), 'W') ==> 15-APR-1994
TO_CHAR (ROUND (TO_DATE ('16-APR-1994'), 'W'), 'DAY') ==> FRIDAY使用To_char 和Round 组合显示日期:
Round back to nearest day (time always midnight):
TO_CHAR (ROUND (TO_DATE ('11-SEP-1994 10:00 AM',
'DD-MON-YY HH:MI AM'), 'DD'),
'DD-MON-YY HH:MI AM')
==> 11-SEP-1994 12:00 AM
Round forward to the nearest day:
TO_CHAR (ROUND (TO_DATE ('11-SEP-1994 4:00 PM',
'DD-MON-YY HH:MI AM'), 'DD'),
'DD-MON-YY HH:MI AM')
==> 12-SEP-1994 12:00 AM
Round back to the nearest hour:
TO_CHAR (ROUND (TO_DATE ('11-SEP-1994 4:17 PM',
'DD-MON-YY HH:MI AM'), 'HH'),
'DD-MON-YY HH:MI AM')
==> 11-SEP-1994 04:00 PM
经常会用到的一些Trunc 函数例子(默认的日期格式是DD-MON-YYYY);
Without a format mask, TRUNC sets the time to 12:00 A.M. of the same day:
TO_CHAR (TRUNC (TO_DATE ('11-SEP-1994 9:36 AM', 'DD-MON-YYYY HH:MI AM'))
==> 11-SEP-1994 12:00 AM
Trunc to the beginning of the century in all cases:
TO_CHAR (TRUNC (TO_DATE ('01-MAR-1994'), 'CC'), 'DD-MON-YYYY')
==> 01-JAN-1900
TO_CHAR (TRUNC (TO_DATE ('01-MAR-1945'), 'CC'), 'DD-MON-YYYY')
==> 01-JAN-1900
Trunc to the first of the current year:
TRUNC (TO_DATE ('01-MAR-1994'), 'YYYY') ==> 01-JAN-1994
TRUNC (TO_DATE ('01-SEP-1994'), 'YEAR') ==> 01-JAN-1994
Trunc to the first day of the quarter:
TRUNC (TO_DATE ('01-MAR-1994'), 'Q') ==> 01-JAN-1994
TRUNC (TO_DATE ('15-APR-1994'), 'Q') ==> 01-APR-1994
Trunc to the first of the month:
TRUNC (TO_DATE ('12-MAR-1994'), 'MONTH') ==> 01-MAR-1994
TRUNC (TO_DATE ('17-MAR-1994'), 'MM') ==> 01-APR-1994 TO_Char 函数和Trunc 函数的一些组合:
Trunc back to the beginning of the current day (time is always midnight):
TO_CHAR (TRUNC (TO_DATE ('11-SEP-1994 10:00 AM',
'DD-MON-YYYY HH:MI AM'), 'DD'),
'DD-MON-YYYY HH:MI AM')
==> 11-SEP-1994 12:00 AM
TO_CHAR (TRUNC (TO_DATE ('11-SEP-1994 4:00 PM',
'DD-MON-YYYY HH:MI AM'), 'DD'),
'DD-MON-YYYY HH:MI AM')
==> 11-SEP-1994 12:00 AM
Trunc to the beginning of the current hour:
TO_CHAR (TRUNC (TO_DATE ('11-SEP-1994 4:17 PM',
'DD-MON-YYYY HH:MI AM'), 'HH'),
'DD-MON-YYYY HH:MI AM')
==> 11-SEP-1994 04:00 PM
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/34596/viewspace-661937/ ,如需转载,请注明出处,否则将追究法律责任。