Languages
[Edit]
EN

MySQL - combine DATE and TIME column into TIMESTAMP

0 points
Created by:
maxsior322
347

In this article, we would like to show you how to combine DATE and TIME column into TIMESTAMP in MySQL.

Quick solution

SELECT TIMESTAMP(`date_column`, `time_column`) AS 'alias_name'
FROM `table_name`;

Practical example

To show how to combine DATE and TIME column into TIMESTAMP, we will use the following table:

MySQL - example data used to combine DATE and TIME column into TIMESTAMP
MySQL - example data used to combine DATE and TIME column into TIMESTAMP

Note:

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

Example

In this example, we will combine registration_date and registration_time column into one - registration_timestamp.

Query:

SELECT TIMESTAMP(`registration_date`, `registration_time`) AS 'registration_timestamp'
FROM `users`;

Result:

MySQL - combine DATE and TIME column into TIMESTAMP - result
MySQL - combine DATE and TIME column into TIMESTAMP - result

Database preparation

create_tables.sql file:

CREATE TABLE `users` (
	`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`username` VARCHAR(50) NOT NULL,
	`registration_date` DATE NOT NULL,
	`registration_time` TIME NOT NULL,
	PRIMARY KEY (`id`)
);

insert_data.sql file:

INSERT INTO `users`
	(`username`, `registration_date`, `registration_time` )
VALUES
	('Tom', '2021-01-01', '11:41:31'),
	('Chris','2021-01-02', '11:42:45'),
	('Jack','2021-01-03', ' 15:13:39'),
   ('Kim','2021-01-03', '15:24:51'),
   ('Marco','2021-01-04', '22:35:38'),
	('Kate','2021-01-04', '22:46:51'),
	('Nam','2021-01-04', '22:57:37');
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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