Oracle uses ROUND and EXTRACT for date operations:
- SQL>SelectTo_char (sysdate,'Yyyy/mm/dd hh24: mi: ss')FromDual;
- TO_CHAR (SYSDATE, 'yyyy/MM/DDHH2
- ------------------------------
- 18:13:20
ROUND (d, [fmt]): returns a rounding date value in the fmt Format.
D is the date, fmt is the format model, and cannot be the hour, minute, and second. The default value is DDD, that is, one day of the month.
- If fmt is "YEAR", it is rounded to January 1 of a YEAR, that is, the first half of the YEAR, and the last half of the YEAR is used as the next YEAR.
- SQL>SelectTo_char (round (sysdate,'Year'),'Yyyy/mm/dd hh24: mi: ss')FromDual;
- TO_CHAR (ROUND (SYSDATE,'Year'),
- ------------------------------
- 00:00:00
- If fmt is "MONTH", it is rounded to the day of a MONTH, that is, the first half of the MONTH, and the second half of the MONTH as the next MONTH.
- SQL> select to_char (round (sysdate,'Month'),'Yyyy/mm/dd hh24: mi: ss') From dual;
- TO_CHAR (ROUND (SYSDATE,'Month')
- ------------------------------
- 2012/04/01 00:00:00
- If fmt is "DDD", that is, a day in the month, the day closest to the day, the first half of the day will be taken away, and the next half day will be taken as the next day.
- SQL>SelectTo_char (round (sysdate,'Ddd'),'Yyyy/mm/dd hh24: mi: ss')FromDual;
- TO_CHAR (ROUND (SYSDATE,'Ddd'),'
- ------------------------------
- 00:00:00
- If fmt is "DAY", it is rounded to Sunday of the last week, that is, the first half of the week is taken away, and the second half is used as the next week.
- SQL> select to_char (round (sysdate,'Day'),'Yyyy/mm/dd hh24: mi: ss') From dual;
- TO_CHAR (ROUND (SYSDATE,'Day'),'
- ------------------------------
- 2012/04/01 00:00:00
EXTRACT (fmt FROM d): EXTRACT a specific part of a date.
Fmt is: YEAR, MONTH, DAY, HOUR, SECOND. YEAR, MONTH, and DAY can be of the DATA type or TIMESTAMP type. However, HOUR, MINUTE, and SECOND must be of the TIMESTAMP type.
- SQL>SelectTo_char (sysdate,'Yyyy/mm/dd hh24: mi: ss')"Date",
- 2 extract (YEAR FROMSYSDATE)"Year",
- 3 extract (MONTH FROMSYSDATE)"Month",
- 4 extract (DAY FROMSYSDATE)"Day",
- 5 extract (HOUR FROMSYSTIMESTAMP)"Hour",
- 6 extract (MINUTE FROMSYSTIMESTAMP)"Minute",
- 7 extract (SECOND FROMSYSTIMESTAMP)"Second"
- 8FromDual;
- -- The result of HOUR matching is not added with the time zone, so the result of running in China is 8 hours less
- DateYearMonthDayHourMinuteSecond
- -------------------------------------------------------------------
- 18:53:42 2012 4 3 10 53 42.79
For more information about Oracle, see Oracle topics page http://www.bkjia.com/topicnews.aspx? Tid = 12