MySQL SECOND

The MySQL SECOND function is a date and time function that is used to extract the seconds component from a given time expression. It is particularly useful when you need to retrieve or manipulate specific parts of a time value. The basic syntax of the SECOND function is as follows:

Syntax

SECOND(time_expression)

Here, time_expression is the time value from which you want to extract the seconds. It can be a valid date, time, or datetime expression.

Example

Let’s look at a simple example to illustrate how to use the SECOND function:

SELECT SECOND('2024-01-01 12:34:56');

In this example, the SECOND function is applied to the datetime value ‘2024-01-01 12:34:56’, and it returns the seconds component, which is 56.

You can also use the SECOND function with other date and time functions or expressions to perform more complex operations. For instance:

SELECT
  CONCAT(HOUR('2024-01-01 12:34:56'), ':', MINUTE('2024-01-01 12:34:56'), ':', SECOND('2024-01-01 12:34:56')) AS formatted_time;

In this example, the CONCAT function is used to combine the hour, minute, and second components of the given datetime value into a formatted time string.

It’s important to note that the SECOND function returns an integer value in the range of 0 to 59, representing the seconds. If the input time expression is NULL, the function will also return NULL.

In summary, the MySQL SECOND function provides a simple and effective way to extract the seconds component from a given time expression, enabling developers to work with and manipulate time data more efficiently in their queries.