How the SECOND() function works in Mariadb?
The SECOND() function in MariaDB is used to extract the seconds part from a TIME or DATETIME value.
The SECOND() function in MariaDB is used to extract the seconds part from a TIME or DATETIME value. This function is particularly useful when you need to work with or manipulate the seconds component of a time-related value.
Syntax
The syntax for the MariaDB SECOND() function is as follows:
SECOND(time)
time: ATIMEorDATETIMEvalue from which the seconds part needs to be extracted. It can be a literal value or an expression that evaluates to a valid time or datetime value.
The function returns an integer value representing the seconds part of the provided time value, ranging from 0 to 59.
Examples
Example 1: Extract seconds from a TIME value
This example demonstrates how to extract the seconds part from a TIME value.
SELECT SECOND('10:05:23');
The output for this statement is:
+--------------------+
| SECOND('10:05:23') |
+--------------------+
| 23 |
+--------------------+The function extracts the seconds part (23) from the provided TIME value '10:05:23'.
Example 2: Extract seconds from a DATETIME value
The SECOND() function can also be used with DATETIME values.
SELECT SECOND('2023-05-15 14:30:45');
The output for this statement is:
+-------------------------------+
| SECOND('2023-05-15 14:30:45') |
+-------------------------------+
| 45 |
+-------------------------------+In this case, the function extracts the seconds part (45) from the DATETIME value '2023-05-15 14:30:45'.
Example 3: Extract seconds from a column
You can use the SECOND() function to extract the seconds part from a time-related column in a table.
DROP TABLE IF EXISTS events;
CREATE TABLE events (
event_id INT PRIMARY KEY,
event_name VARCHAR(50),
event_time DATETIME
);
INSERT INTO events (event_id, event_name, event_time) VALUES
(1, 'Meeting', '2023-06-01 09:15:30'),
(2, 'Presentation', '2023-06-01 13:45:15'),
(3, 'Webinar', '2023-06-01 18:00:00');
SELECT event_name, SECOND(event_time) AS event_seconds
FROM events;
The output for this statement is:
+--------------+---------------+
| event_name | event_seconds |
+--------------+---------------+
| Meeting | 30 |
| Presentation | 15 |
| Webinar | 0 |
+--------------+---------------+In this example, the SECOND() function extracts the seconds part from the event_time column, and the result is displayed in the event_seconds column.
Example 4: Extract seconds from a calculation
You can use the SECOND() function with expressions that evaluate to a valid time or datetime value.
SELECT SECOND(ADDTIME('10:00:00', '00:05:30'));
The output for this statement is:
+-----------------------------------------+
| SECOND(ADDTIME('10:00:00', '00:05:30')) |
+-----------------------------------------+
| 30 |
+-----------------------------------------+In this example, the ADDTIME() function is used to add 5 minutes and 30 seconds to the time '10:00:00', resulting in '10:05:30'. The SECOND() function then extracts the seconds part (30) from this calculated time value.
Example 5: Extract seconds from NULL value
If the SECOND() function is provided with a NULL value, it returns NULL.
SELECT SECOND(NULL);
The output for this statement is:
+--------------+
| SECOND(NULL) |
+--------------+
| NULL |
+--------------+Related Functions
Here are a few functions related to the MariaDB SECOND() function:
- MariaDB
MINUTE()function is used to extract the minutes part from aTIMEorDATETIMEvalue. - MariaDB
HOUR()function is used to extract the hours part from aTIMEorDATETIMEvalue. - MariaDB
MICROSECOND()function is used to extract the microseconds part from aTIMEorDATETIMEvalue.
Conclusion
The SECOND() function in MariaDB is a handy tool for extracting the seconds part from TIME or DATETIME values. By understanding the syntax and usage of this function, you can effectively manipulate and work with time-related data in your MariaDB database. Whether you need to extract seconds from a literal value, a column, or a calculated expression, the SECOND() function provides a straightforward way to accomplish this task.