Table 5-14 lists the format models you can use with the ROUND and TRUNC date functions and the units to which they round and truncate dates. The default model, 'DD', returns the date rounded or truncated to the day with a time of midnight.
Table 5-14 Date Format Models for the ROUND and TRUNC Date Functions
Format Model
Rounding or Truncating Unit
CC
SCC
One greater than the first two digits of a four-digit year
SYYYY
YYYY
YEAR
SYEAR
YYY
YY
Y
Year (rounds up on July 1)
IYYY
IY
IY
I
ISO Year
Q
Quarter (rounds up on the sixteenth day of the second month of the quarter)
MONTH
MON
MM
RM
Month (rounds up on the sixteenth day)
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
EXTRACT( { { YEAR
| MONTH
| DAY
| HOUR
| MINUTE
| SECOND
}
| { TIMEZONE_HOUR
| TIMEZONE_MINUTE
}
| { TIMEZONE_REGION
| TIMEZONE_ABBR
}
} FROM { datetime_value_expression
| interval_value_expression
}
) 例句:
The following example returns from the oe.orders table the number of orders placed in each month:
SELECT EXTRACT(month FROM order_date) "Month",
COUNT(order_date) "No. of Orders"
FROM orders
GROUP BY EXTRACT(month FROM order_date)
ORDER BY "No. of Orders" DESC;
Month No. of Orders
---------- -------------
11 15
7 14
6 14
3 11
5 10
9 9
2 9
8 7
10 6
1 5
12 4
4 1
12 rows selected.
The following example returns the year 1998.
SELECT EXTRACT(YEAR FROM DATE '1998-03-07') FROM DUAL;
EXTRACT(YEARFROMDATE'1998-03-07')
---------------------------------
1998
The following example selects from the sample table hr.employees all employees who were hired after 1998:
SELECT last_name, employee_id, hire_date
FROM employees
WHERE EXTRACT(YEAR FROM
TO_DATE(hire_date, 'DD-MON-RR')) > 1998
ORDER BY hire_date;