The SECOND() function in MySQL is used to return the seconds part from a given time or datetime value. The result will be a number between 0 and 59.
Definition and Usage
- Purpose: Returns the seconds part of a given time or datetime.
- Range: 0 to 59.
Syntax
SECOND(datetime)
Parameters
- datetime: A time or datetime value from which the second is extracted.
Technical Details
- Works in: MySQL 4.0 and later.
- Return Type: Integer (0 to 59).
Examples
Example 1: Extract the Seconds from a Datetime
SELECT SECOND("2017-06-20 09:34:00.000023");
Result: 0
Explanation: The seconds part of the given datetime is 00.
Example 2: Extract the Seconds from a Time Value
SELECT SECOND("23:59:59");
Result: 59
Explanation: The seconds part of the time 23:59:59 is 59.
Use Cases
- Time Tracking: For extracting and processing the second part of a datetime value (useful in timestamps or event logging).
- Time Calculations: When performing operations or comparisons based on time down to the second.
- Analytics: Extract seconds from timestamps for analysis related to time-based data (e.g., transactions per second).
Key Notes
- The function works for both time and datetime types.
- If a value is in the wrong format or contains an invalid time, the query will return an error.