
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to convert DateTime to a number in MySQL?
To convert the date time to a number in MySQL, the syntax is as follows −
SELECT UNIX_TIMESTAMP(yourColumnName) as anyVariableName FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table DateTimeToNumberDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> releasedDate datetime, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.46 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into DateTimeToNumberDemo(releasedDate) values(now()); Query OK, 1 row affected (0.19 sec) mysql> insert into DateTimeToNumberDemo(releasedDate) values(curdate()); Query OK, 1 row affected (0.24 sec) mysql> insert into DateTimeToNumberDemo(releasedDate) values('1978-01-19'); Query OK, 1 row affected (0.55 sec) mysql> insert into DateTimeToNumberDemo(releasedDate) values('2016-09-13'); Query OK, 1 row affected (0.46 sec) mysql> insert into DateTimeToNumberDemo(releasedDate) values('2017-11-12'); Query OK, 1 row affected (0.22 sec) mysql> insert into DateTimeToNumberDemo(releasedDate) values('2018-12-09'); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from DateTimeToNumberDemo;
The following is the output −
+----+---------------------+ | Id | releasedDate | +----+---------------------+ | 1 | 2019-01-12 21:20:57 | | 2 | 2019-01-12 00:00:00 | | 3 | 1978-01-19 00:00:00 | | 4 | 2016-09-13 00:00:00 | | 5 | 2017-11-12 00:00:00 | | 6 | 2018-12-09 00:00:00 | +----+---------------------+ 6 rows in set (0.00 sec)
The following is the query to convert date time to a number −
mysql> select unix_timestamp(releasedDate) as DateToNumber from DateTimeToNumberDemo;
The following is the output −
+--------------+ | DateToNumber | +--------------+ | 1547308257 | | 1547231400 | | 253996200 | | 1473705000 | | 1510425000 | | 1544293800 | +--------------+ 6 rows in set (0.00 sec)
- Related Articles
- How to convert JavaScript datetime to MySQL datetime?
- How to convert timestamp to datetime in MySQL?
- How to convert MySQL datetime to Unix timestamp?
- Convert INT to DATETIME in MySQL?
- How to convert char field to datetime field in MySQL?
- How to convert JS date time to MySQL datetime?
- How to convert string to 24-hour datetime format in MySQL?
- How to convert MySQL DATETIME value to JSON format in JavaScript?
- Convert datetime to get month name in MySQL?
- MySQL Query to convert from datetime to date?
- Convert from varchar to datetime and compare in MySQL?
- How to Convert Text Datetime Format to Real Datetime Format in Excel?
- How to convert date to datetime in Python?
- How can we convert TIME and DATETIME values to numeric form in MySQL?
- How to Convert yyyymmddhhmmss Date Format to Normal Datetime in Excel?Datetime in Excel?

Advertisements