Display only date from timestamp value in MySQL

MySQLMySQLi Database

To display the only date from timestamp value, use the FROM_UNIXTIME() method in MySQL. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> timestampValue bigint
   -> );
Query OK, 0 rows affected (0.70 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(1538332200);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(1577730600);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(1488652200);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------------+
| timestampValue |
+----------------+
|     1538332200 |
|     1577730600 |
|     1488652200 |
+----------------+
3 rows in set (0.00 sec)

Following is the query to display date from timestamp value −

mysql> select from_unixtime(timestampValue) as Date from DemoTable;

This will produce the following output −

+---------------------+
| Date                |
+---------------------+
| 2018-10-01 00:00:00 |
| 2019-12-31 00:00:00 |
| 2017-03-05 00:00:00 |
+---------------------+
3 rows in set (0.00 sec)
raja
Published on 17-Dec-2019 12:31:13
Advertisements