最新的Web開發教程
 

MySQL的EXTRACT() Function


<MySQL的日期函數

定義和用法

EXTRACT()函數是用於返回的日期/時間的單個部件,如年,月,日,時,分,等

Syntax

EXTRACT(unit FROM date)

凡日期是有效的日期表達式和單位可以是以下之一:

Unit Value
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 2014-11-22 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

會導致這樣的:

OrderYearOrderMonthOrderDay
20141122

<MySQL的日期函數