MySQL EXTRACT函数
时间:2019-02-04 12:52:44 来源:igfitidea点击:
本MySQL教程通过语法和示例说明了如何使用MySQL EXTRACT函数。
描述
MySQL EXTRACT函数从日期中提取特定部分的值。
语法
MySQL EXTRACT函数的语法为:
EXTRACT( unit FROM date )
date日期或者日期时间值。
unit提取的部分
可以是以下值之一:
| 单位 | 兼容性 |
|---|---|
| MICROSECOND | MySQL 4.1.1+ |
| SECOND | MySQL 3.2.3+ |
| MINUTE | MySQL 3.2.3+ |
| HOUR | MySQL 3.2.3+ |
| DAY | MySQL 3.2.3+ |
| WEEK | MySQL 5+ |
| MONTH | MySQL 3.2.3+ |
| QUARTER | MySQL 5+ |
| YEAR | MySQL 3.2.3+ |
| SECOND_MICROSECOND | MySQL 4.1.1+ |
| MINUTE_MICROSECOND | MySQL 4.1.1+ |
| MINUTE_SECOND | MySQL 4.1.1+ |
| HOUR_MICROSECOND | MySQL 4.1.1+ |
| HOUR_SECOND | MySQL 4.1.1+ |
| HOUR_MINUTE | MySQL 3.2.3+ |
| DAY_MICROSECOND | MySQL 4.1.1+ |
| DAY_SECOND | MySQL 3.2.3+ |
| DAY_MINUTE | MySQL 3.2.3+ |
| DAY_HOUR | MySQL 3.2.3+ |
| YEAR_MONTH | MySQL 3.2.3+ |
示例
mysql> SELECT EXTRACT(MICROSECOND FROM '2012-05-13 10:36:21.000001'); Result: 1 mysql> SELECT EXTRACT(SECOND FROM '2012-05-13 10:36:21'); Result: 21 mysql> SELECT EXTRACT(MINUTE FROM '2012-05-13 10:36:21'); Result: 36 mysql> SELECT EXTRACT(HOUR FROM '2012-05-13 10:36:21'); Result: 10

