The MySQL SECOND()
function is used to return the second component from a time value.
The return value for this function is in the range 0 to 59. Below are examples to demonstrate.
Syntax
The syntax of this function goes like this:
SECOND(time)
Where time
is the time value that you want to extract the seconds component from.
Example 1 – Basic Usage
Here’s an example to demonstrate.
SELECT SECOND('10:35:27');
Result:
+--------------------+ | SECOND('10:35:27') | +--------------------+ | 27 | +--------------------+
Example 2 – Abbreviated Time Values without Colons
Here’s an example using an abbreviated time value without colons.
SELECT SECOND('1227');
Result:
+----------------+ | SECOND('1227') | +----------------+ | 27 | +----------------+
And here’s an example where only two digits are provided.
SELECT SECOND('27');
Result:
+--------------+ | SECOND('27') | +--------------+ | 27 | +--------------+
So MySQL interprets this as being 27 seconds.
However, be careful when using abbreviated time values, as MySQL can sometimes interpret them differently to what you might expect.
Here’s what the MySQL documentation says about this:
Be careful about assigning abbreviated values to a
TIME
column. MySQL interprets abbreviatedTIME
values with colons as time of the day. That is,'11:12'
means'11:12:00'
, not'00:11:12'
. MySQL interprets abbreviated values without colons using the assumption that the two rightmost digits represent seconds (that is, as elapsed time rather than as time of day). For example, you might think of'1112'
and1112
as meaning'11:12:00'
(12 minutes after 11 o’clock), but MySQL interprets them as'00:11:12'
(11 minutes, 12 seconds). Similarly,'12'
and12
are interpreted as'00:00:12'
.
Example 3 – An Alternative
You can also use the EXTRACT()
function to extract the seconds (and other date/time parts) from a date/time value:
SELECT EXTRACT(SECOND FROM '10:35:27');
Result:
+---------------------------------+ | EXTRACT(SECOND FROM '10:35:27') | +---------------------------------+ | 27 | +---------------------------------+