How to select date from timestamp in MySQL?


To select date from timestamp in MySQL, you need to use DATE().

Let us first create a table −

mysql> create table DemoTable697(
   Id varchar(100),
   Title varchar(100),
   BatchTime timestamp
);
Query OK, 0 rows affected (0.78 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable697 values('10','Java','2019-01-21 10:34:56');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable697 values('11','Spring','2019-03-11 11:14:16');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable697 values('12','Hibernate','2019-07-21 12:04:00');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable697;

This will produce the following output -

+------+-----------+---------------------+
| Id   | Title     | BatchTime           |
+------+-----------+---------------------+
| 10   | Java      | 2019-01-21 10:34:56 |
| 11   | Spring    | 2019-03-11 11:14:16 |
| 12   | Hibernate | 2019-07-21 12:04:00 |
+------+-----------+---------------------+
3 rows in set (0.00 sec)

Here is the query to select date from timestamp in MySQL −

mysql> select Title,DATE(BatchTime) AS OnlyDate from DemoTable697 where Id='10';

This will produce the following output -

+-------+------------+
| Title | OnlyDate   |
+-------+------------+
| Java  | 2019-01-21 |
+-------+------------+
1 row in set (0.00 sec)

Updated on: 21-Aug-2019

362 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements