Collections:
EXTRACT() - Extracting Datetime Component
How to extract components from a given datetime using the EXTRACT() function?
✍: FYIcenter.com
EXTRACT(unit FROM date) is a MySQL built-in function that
returns components from a given datetime.
For example:
SELECT EXTRACT(YEAR FROM '2023-07-02 10:30:55.000123'); -- +-------------------------------------------------+ -- | EXTRACT(YEAR FROM '2023-07-02 10:30:55.000123') | -- +-------------------------------------------------+ -- | 2023 | -- +-------------------------------------------------+ SELECT EXTRACT(YEAR_MONTH FROM '2023-07-02 10:30:55.000123'); -- +-------------------------------------------------------+ -- | EXTRACT(YEAR_MONTH FROM '2023-07-02 10:30:55.000123') | -- +-------------------------------------------------------+ -- | 202307 | -- +-------------------------------------------------------+ SELECT EXTRACT(DAY_MINUTE FROM '2023-07-02 10:30:55.000123'); -- +-------------------------------------------------------+ -- | EXTRACT(DAY_MINUTE FROM '2023-07-02 10:30:55.000123') | -- +-------------------------------------------------------+ -- | 21030 | -- +-------------------------------------------------------+ SELECT EXTRACT(MICROSECOND FROM '2023-07-02 10:30:55.000123'); -- +--------------------------------------------------------+ -- | EXTRACT(MICROSECOND FROM '2023-07-02 10:30:55.000123') | -- +--------------------------------------------------------+ -- | 123 | -- +--------------------------------------------------------+
Reference information of the EXTRACT() function:
EXTRACT(unit FROM date): int Extracts parts specified by unit keyword from the date. Arguments, return value and availability: unit: Required. Components to extract as datetime internal unit keyword. date: Required. The date to extract components from. int: Return value. Extracted components concatenated as an integer. Available since MySQL 4. Datetime interval unit keywords: Unit keyword Output integer format ------------------ --------------------- MICROSECOND ffffff SECOND ss MINUTE ii HOUR HH DAY dd WEEK VV MONTH mm QUARTER q YEAR YYYY SECOND_MICROSECOND ssffffff MINUTE_MICROSECOND iissffffff MINUTE_SECOND iiss HOUR_MICROSECOND HHiissffffff HOUR_SECOND HHiiss HOUR_MINUTE HHii DAY_MICROSECOND ddHHiissffffff DAY_SECOND ddHHiiss DAY_MINUTE ddHHii DAY_HOUR ddHH YEAR_MONTH YYYYmm
⇒ FORMAT_PICO_TIME() - Formatting Picoseconds in Readable Units
2023-11-17, 1089🔥, 0💬
Popular Posts:
How To Start the Command-Line SQL*Plus in Oracle? If you Oracle server or client installed on your w...
How To Start the Command-Line SQL*Plus in Oracle? If you Oracle server or client installed on your w...
How To Use GO Command in "sqlcmd" in SQL Server? "sqlcmd" is a command line client application to ru...
How To Locate and Take Substrings with CHARINDEX() and SUBSTRING() Functions in SQL Server Transact-...
What Happens If the UPDATE Subquery Returns Multiple Rows in MySQL? If a subquery is used in a UPDAT...