Jump to: navigation, search

Mysql EXTRACT

From w3cyberlearnings

Contents

MySQL EXTRACT Function

This function extracts parts from the date.

Syntax EXTRACT

  • unit format
  • X is date or datetime expression.
EXTRACT(unit, X)

Format

unit Value	Expected expr Format
----------------------------------------
MICROSECOND	MICROSECONDS
SECOND	        SECONDS
MINUTE	        MINUTES
HOUR	        HOURS
DAY	        DAYS
WEEK	        WEEKS
MONTH	        MONTHS
QUARTER	        QUARTERS
YEAR	        YEARS
SECOND_MICROSECOND	'SECONDS.MICROSECONDS'
MINUTE_MICROSECOND	'MINUTES:SECONDS.MICROSECONDS'
MINUTE_SECOND	        'MINUTES:SECONDS'
HOUR_MICROSECOND	'HOURS:MINUTES:SECONDS.MICROSECONDS'
HOUR_SECOND	'HOURS:MINUTES:SECONDS'
HOUR_MINUTE	'HOURS:MINUTES'
DAY_MICROSECOND	'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'
DAY_SECOND	'DAYS HOURS:MINUTES:SECONDS'
DAY_MINUTE	'DAYS HOURS:MINUTES'
DAY_HOUR	'DAYS HOURS'
YEAR_MONTH	'YEARS-MONTHS'

Example 1

mysql> SELECT EXTRACT(QUARTER FROM '2012-04-12');
+------------------------------------+
| EXTRACT(QUARTER FROM '2012-04-12') |
+------------------------------------+
|                                  2 | 
+------------------------------------+
1 row in set (0.00 sec)

Example 2

mysql> SELECT EXTRACT(YEAR FROM '2012-04-12');
+---------------------------------+
| EXTRACT(YEAR FROM '2012-04-12') |
+---------------------------------+
|                            2012 | 
+---------------------------------+
1 row in set (0.00 sec)

Related Links



Navigation
Web
SQL
MISC
References