In MariaDB, SECOND() is a built-in date and time function that returns the seconds portion of a given time expression.
It accepts one argument, which is the time you want to extract the seconds from.
The seconds are returned as a number in the range 0 to 59.
Syntax
The syntax goes like this:
SECOND(time)
Where time is the time expression to get the seconds from.
Example
Here’s an example:
SELECT SECOND('10:30:45');
Result:
+--------------------+
| SECOND('10:30:45') |
+--------------------+
| 45 |
+--------------------+
Here’s another example that includes microseconds:
SELECT SECOND('10:30:45.123456');
Result:
+---------------------------+
| SECOND('10:30:45.123456') |
+---------------------------+
| 45 |
+---------------------------+
Either way, the result is the same.
Datetime Values
It also works with datetime values:
SELECT SECOND('2030-02-01 10:30:45');
Result:
+-------------------------------+
| SECOND('2030-02-01 10:30:45') |
+-------------------------------+
| 45 |
+-------------------------------+
Current Date
We can pass NOW() as the datetime argument to use the current time:
SELECT
NOW(),
SECOND(NOW());
Result:
+---------------------+---------------+ | NOW() | SECOND(NOW()) | +---------------------+---------------+ | 2021-05-16 14:32:40 | 40 | +---------------------+---------------+
Invalid Arguments
When passed an invalid time argument, SECOND() returns null:
SELECT SECOND('10:75:00');
Result:
+--------------------+
| SECOND('10:75:00') |
+--------------------+
| NULL |
+--------------------+
Missing Argument
Calling SECOND() with the wrong number of arguments, or without passing any arguments, results in an error:
SELECT SECOND();
Result:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1
And another example:
SELECT SECOND('10:30:45', '06:30:45');
Result:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' '06:30:45')' at line 1