EXTRACT() : Parts of Date & Time in MySQL![]()
Output is 2017 Input format must be in "YYYY-MM-DD HH-MM-SS" , we can use STR_TO_DATE() for any other format ( See examples below )
Output 28
Output 28
SELECT EXTRACT( MONTH FROM '2017-10-10' );Output is 10 SELECT EXTRACT( YEAR_MONTH FROM '2017-03-08' )Output is 201703 EXTRACT() with STR_TO_DATE()We can use other type input date formats also.SELECT EXTRACT( YEAR FROM STR_TO_DATE('5-07-2017', '%d-%m-%Y') ) EXTRACT() with NOW()SELECT EXTRACT( YEAR FROM NOW() )Output is Present Year 2017 SELECT EXTRACT( HOUR FROM NOW( ) )Output is 04 Output is 45
Output is 09
Output is 20
Output is 0445
Output is 4509
EXTRACT() with LOCALTIMESTAMP()
EXTRACT() with CURDATE() Output is 25
Output is 03
SELECT EXTRACT( YEAR FROM CURDATE() )Output is 2023 Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
![]() ▼ More on Date & Time functions in SQL |