mysql date part code example

Example 1: date_part mysql

mysql> SELECT EXTRACT(YEAR FROM '2019-07-02');
        -> 2019
mysql> SELECT EXTRACT(YEAR_MONTH FROM '2019-07-02 01:02:03');
        -> 201907
mysql> SELECT EXTRACT(DAY_MINUTE FROM '2019-07-02 01:02:03');
        -> 20102
mysql> SELECT EXTRACT(MICROSECOND
    ->                FROM '2003-01-02 10:30:00.000123');
        -> 123

Example 2: mysql current date

SELECT NOW();
It returns current date and time.

Example 3: mysql current time

CURRENT_TIME return HH:ii:ss format time in mysql

Example 4: mysql get date from datetime

WHERE DATE(datetime) = '2009-10-20'

Example 5: get individual date elements in mysql

-- To get the year of a date in mysql
SELECT YEAR(NOW());

-- To get the month of a date in mysql
SELECT MONTH(NOW());

-- To get the day of a date in mysql
SELECT DAY(NOW());

-- To get the hour of a date in mysql
SELECT HOUR(NOW());

-- To get the minute of a date in mysql
SELECT MINUTE(NOW());

-- To get the second of a date in mysql
SELECT SECOND(NOW());

Tags:

Sql Example