How to convert yyyymmdd in INT type to date?

MySQLMySQLi Database

For this, you can use the DATE() function. Let us first create a table −

mysql> create table DemoTable
(
   Number int
);
Query OK, 0 rows affected (0.48 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(20190108);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(20161231);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(20170411);
Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+
| Number   |
+----------+
| 20190108 |
| 20161231 |
| 20170411 |
+----------+
3 rows in set (0.00 sec)

Following is the query to convert yyyymmdd in int type to date −

mysql> select date(Number) from DemoTable;

This will produce the following output −

+--------------+
| date(Number) |
+--------------+
| 2019-01-08   |
| 2016-12-31   |
| 2017-04-11   |
+--------------+
3 rows in set (0.00 sec)
raja
Updated on 26-Sep-2019 06:51:43

Advertisements