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, 1060🔥, 0💬
Popular Posts:
What are DDL (Data Definition Language) statements for tables in SQL Server? DDL (Data Definition La...
How To Get the Definition of a Stored Procedure Back in SQL Server Transact-SQL? If you want get the...
What Are Date and Time Functions in MySQL? MySQL offers a number of functions for date and time valu...
How to connect SQL Server Management Studio Express to SQL Server 2005 Express in SQL Server? Once y...
How To Start the Command-Line SQL*Plus in Oracle? If you Oracle server or client installed on your w...