How to use the MySQL SECOND() function

In this article, we will learn how to use the MySQL SECOND() function, which returns the second of a time or datetime value.

Posted on

In this article, we will learn how to use the MySQL SECOND() function, which returns the second of a time or datetime value. We will also see some examples of how to use this function in different situations, and explore some related functions that can be helpful for working with seconds and time values.

Syntax

The syntax of the SECOND() function is as follows:

SECOND(time)

The time parameter can be any valid time or datetime expression, or a string that can be converted to a time or datetime value. If the time parameter is invalid or NULL, the function returns NULL. The SECOND() function returns an integer value from 0 to 59, representing the second of the given time. For example, SECOND('2023-01-15 10:02:34') returns 34.

Examples

Let’s see some examples of how to use the SECOND() function in MySQL.

Example 1: Get the second of a time value

We can use the SECOND() function to get the second of a time value. For example:

SELECT SECOND('10:02:34') AS result;

This query will return the second of the given time value. The query will return 34, since the second of the time value is 34.

Example 2: Get the second of a datetime value

We can use the SECOND() function to get the second of a datetime value. For example:

SELECT SECOND('2023-01-15 10:02:34') AS result;

This query will return the second of the given datetime value. The query will return 34, since the second of the datetime value is 34.

Example 3: Get the second of the current time

We can use the SECOND() function with the CURTIME() function, which returns the current time, to get the second of the current time. For example:

SELECT SECOND(CURTIME()) AS current_second;

This query will return the second of the current time. For example, if the current time is 10:02:34, the query will return 34.

Example 4: Get the second of the current datetime

We can use the SECOND() function with the NOW() function, which returns the current date and time, to get the second of the current datetime. For example:

SELECT SECOND(NOW()) AS current_second;

This query will return the second of the current datetime. For example, if the current date and time is 2023-01-15 10:02:34, the query will return 34.

Example 5: Get the number of records for each second

We can use the SECOND() function with the GROUP BY clause to get the number of records for each second in a table. For example, suppose we have a table called logs that stores the log details of a system, with the following columns:

  • log_id: the unique identifier of the log
  • log_message: the message of the log
  • log_time: the time when the log was generated

We can use the following query to get the number of logs for each second in the table:

SELECT SECOND(log_time) AS second, COUNT(*) AS log_count
FROM logs
GROUP BY second
ORDER BY second;

This query will return the second and the log count for each second in the table, ordered by the second. For example, the query might return something like this:

second log_count
0 5
1 3
2 4
58 6
59 7

There are some other functions that are related to the SECOND() function, and can be useful for working with seconds and time values. Here are some of them:

  • MINUTE(): This function returns the minute of a time or datetime value. For example, MINUTE('2023-01-15 10:02:34') returns 2.
  • HOUR(): This function returns the hour of a time or datetime value. For example, HOUR('2023-01-15 10:02:34') returns 10.
  • TIME_TO_SEC(): This function converts a time value to a number of seconds. For example, TIME_TO_SEC('10:02:34') returns 36154.
  • SEC_TO_TIME(): This function converts a number of seconds to a time value. For example, SEC_TO_TIME(36154) returns ‘10:02:34’.
  • CURTIME(): This function returns the current time as a time value. For example, CURTIME() returns ‘10:02:34’ if the current time is 10:02:34.
  • NOW(): This function returns the current date and time as a datetime value. For example, NOW() returns ‘2023-01-15 10:02:34’ if the current date and time is 2023-01-15 10:02:34.

Conclusion

In this article, we learned how to use the MySQL SECOND() function, which returns the second of a time or datetime value. We also saw some examples of how to use this function in different situations, and explored some related functions that can be helpful for working with seconds and time values.