MySQL - SECOND() Function



The MySQL SECOND() function is used to retrieve and return the seconds value from the given time or date time expression.

The SECOND() function accepts the time string as an argument and retrieves the seconds part of it. Thus, the range of result always lies between 0 to 59.

Note: This function does not calculate the duration from the given time in the form of seconds, but just gets the seconds from the timestamp argument passed to it.

Syntax

Following is the syntax of MySQL SECOND() function −

SECOND(time);

Parameters

This method accepts the time expression from which you need to extract the hour as a parameter.

Return value

This function returns the seconds value from the given time or date time expression.

Example

In the following query, we are using the MySQL SECOND() function to fetch the seconds value from the given datetime value −

SELECT SECOND('2015-09-05 09:40:45.2300') As Result;

Output

This will produce the following result −

Result
45

Example

Here, we are retrieving the seconds value from the current timestamp −

SELECT SECOND(CURRENT_TIMESTAMP) As Result;

Output

Following is the output −

Result
58

Example

We can pass the result of the NOW() function as an argument to this function −

SELECT SECOND(NOW()) As Result;

Output

Following is the output −

Result
58

Example

In this example, we have created a table named ORDERS using the following CREATE TABLE query −

CREATE TABLE ORDERS (
   OID INT NOT NULL,
   DATE VARCHAR (20) NOT NULL,
   CUSTOMER_ID INT NOT NULL,
   AMOUNT DECIMAL (18, 2)
);

Now, insert the following records into the ORDERS table using the INSERT statement −

INSERT INTO ORDERS VALUES 
(102, '2009-10-08 00:00:00', 3, 3000.00),
(100, '2009-10-08 00:00:00', 3, 1500.00),
(101, '2009-11-20 00:00:00', 2, 1560.00),
(103, '2008-05-20 00:00:00', 4, 2060.00);

Execute the below query to fetch all the inserted records in the above-created table −

Select * From ORDERS;

Following is the ORDERS table −

OID DATE CUSTOMER_ID AMOUNT
102 2009-10-08 00:00:00 3 3000.00
100 2009-10-08 00:00:00 3 1500.00
101 2009-11-20 00:00:00 2 1560.00
103 2008-05-20 00:00:00 4 2060.00

Now, we use the MySQL SECOND() function to extract the seconds component from all the datetime values in the DATE column of ORDERS table −

SELECT OID, DATE, SECOND(DATE) As Seconds FROM ORDERS;

Output

The output is displayed as follows −

OID DATE Seconds
102 2009-10-08 00:00:00 0
100 2009-10-08 00:00:00 0
101 2009-11-20 00:00:00 0
103 2008-05-20 00:00:00 0
Advertisements