MySQL query to select date from timestamp?


Let us first create a table. One of the column is a timestamp −

mysql> create table DemoTable
   -> (
   -> CustomerName varchar(100),
   -> CustomerShippingDate timestamp
   -> );
Query OK, 0 rows affected (0.52 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Chris','2019-01-21');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable values('David','2019-03-01');
Query OK, 1 row affected (0.22 sec)

mysql> insert into DemoTable values('Robert','2019-06-04');
Query OK, 1 row affected (0.25 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+----------------------+
| CustomerName | CustomerShippingDate |
+--------------+----------------------+
| Chris        | 2019-01-21 00:00:00  |
| David        | 2019-03-01 00:00:00  |
| Robert       | 2019-06-04 00:00:00  |
+--------------+----------------------+
3 rows in set (0.00 sec)

Following is the query to select date on timestamp −

mysql> select *,date(CustomerShippingDate) from DemoTable;

This will produce the following output −

+--------------+----------------------+----------------------------+
| CustomerName | CustomerShippingDate | date(CustomerShippingDate) |
+--------------+----------------------+----------------------------+
| Chris        | 2019-01-21 00:00:00 | 2019-01-21                  |
| David        | 2019-03-01 00:00:00 | 2019-03-01                  |
| Robert       | 2019-06-04 00:00:00 | 2019-06-04                  |
+--------------+----------------------+----------------------------+
3 rows in set (0.00 sec)
raja
Published on 11-Jul-2019 16:40:08
Advertisements