定义和用法
EXTRACT() 函数用于返回日期/时间的单独部分,比如年、月、日、小时、分钟等等。
语法
EXTRACT(unit FROM date)
date 参数是合法的日期表达式。unit 参数可以是下列的值:
Unit 值 |
MICROSECOND |
|
SECOND |
|
MINUTE |
|
HOUR |
|
DAY |
|
WEEK |
|
MONTH |
|
QUARTER |
|
YEAR |
|
SECOND_MICROSECOND |
|
MINUTE_MICROSECOND |
|
MINUTE_SECOND |
|
HOUR_MICROSECOND |
|
HOUR_SECOND |
|
HOUR_MINUTE |
|
DAY_MICROSECOND |
|
DAY_SECOND |
|
DAY_MINUTE |
|
DAY_HOUR |
|
YEAR_MONTH |
|
实例
假设我们有如下的 "Orders" 表:
OrderId |
ProductName |
OrderDate |
1 |
Jarlsberg Cheese |
2008-11-11 13:23:44.657 |
下面是 SELECT 语句:
SELECT EXTRACT(YEAR FROM OrderDate) AS OrderYear,
EXTRACT(MONTH FROM OrderDate) AS OrderMonth,
EXTRACT(DAY FROM OrderDate) AS OrderDay,
FROM Orders
WHERE OrderId=1
结果如下所示:
OrderYear |
OrderMonth |
OrderDay |
2008 |
11 |
11 |