Languages
[Edit]
EN

MySQL - extract SECOND from DATETIME / TIMESTAMP / TIME

0 points
Created by:
Dirask Community
6240

In this article, we would like to show you how to extract SECOND from DATETIME, TIMESTAMP or TIME in MySQL.

Quick solution:

SELECT SECOND(`column_name`) AS 'alias_name'
FROM `table_name`;

Practical example

To show how to extract SECOND from DATETIME, TIMESTAMP or TIME, we will use the following table:

MySQL - example data used to extract SECOND from DATETIME, TIMESTAMP or TIME
MySQL - example data used to extract SECOND from DATETIME, TIMESTAMP or TIME

Note:

At the end of this article you can find database preparation SQL queries.

Example

In this example, we will select only seconds from every record in event_datetime column from events table.

Query:

SELECT SECOND(`event_datetime`) AS 'seconds'
FROM `events`;

Output:

MySQL - extract SECOND from DATETIME, TIMESTAMP or TIME - result
MySQL - extract SECOND from DATETIME, TIMESTAMP or TIME - result

Database preparation

create_tables.sql file:

CREATE TABLE `events`(
	`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`event_datetime` DATETIME,
	PRIMARY KEY (`id`)
);

insert_data.sql file:

INSERT INTO `events`
	(`event_datetime`) 
VALUES 
	('2021-01-01 11:41:31'),
	('2021-01-02 12:42:32'),
	('2021-01-03 13:43:33'),
	('2021-01-04 14:44:34'),
	('2021-01-05 15:45:35'),
	('2021-01-06 16:46:36'),
	('2021-01-07 17:47:37'),
	('2021-01-08 18:48:38'),
	('2021-01-09 19:49:39'),
	('2021-01-10 20:50:40');

MySQL - dates

Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join