MySQL SECOND() Function

MySQL SECOND() function; In this tutorial, i am going to show you MySQL second() function with the help of examples.

MySQL SECOND() Function

SECOND() function in MySQL is used to return the second portion of a specified time or date-time value. The first parameter in this function will be the date/Date Time. This function returns the seconds from the given date value. The return value (seconds) will be in the range of 0 to 59.

Syntax of MySQL SECOND() Function

The syntax of the second() of MySQL is below:

SECOND(time)

Here,time is the time value that you want to extract the seconds component from.

Example 1 – MySQL SECOND() Function

Let’s take a basic example of a second() function is below:

SELECT SECOND('10:35:29');

Output-1

+--------------------+
| SECOND('10:35:29') |
+--------------------+
|                 29 |
+--------------------+

Example 2 – MySQL SECOND() Function

Let’s take an second example using an abbreviated time value without colons; as follows:

SELECT SECOND('1220');

Output-2

+----------------+
| SECOND('1220') |
+----------------+
|             20 |
+----------------+

Example 3 – MySQL SECOND() Function

To EXTRACT () function to extract seconds (and other date / time parts) from the date / time value; as follows:

SELECT EXTRACT(SECOND FROM '10:35:27');

Output-3

+---------------------------------+
| EXTRACT(SECOND FROM '10:35:45') |
+---------------------------------+
|                              45 |
+---------------------------------+

Example 4 – MySQL SECOND() with Now() Function

Let’s take an another example of second() function with NOW() function; as follows:

SELECT SECOND(NOW());
+----------------+
| SECOND(NOW())  |
+----------------+
|             10 |
+----------------+

Let’s look at some more MySQL SECOND() examples and explore how to use the SECOND() in MySQL. See the below more examples :

mysql> SELECT SECOND('2014-01-28 07:47:18.000004');
Result: 18

mysql> SELECT SECOND('2014-01-28 15:21:05');
Result: 5

mysql> SELECT SECOND('12:13:06');
Result: 6

mysql> SELECT SECOND('838:11:59');
Result: 59

Conclusion

Here, you have learned how to use MySQL SECOND() function with various examples.

Leave a Comment