Get only the date from datetime in MySQL?

MySQLMySQLi Database

To get only the date from DateTime, use the date format specifiers −

%d for day
%m for month
%Y for year

Let us first create a table −

mysql> create table DemoTable
(
   AdmissionDate datetime
);
Query OK, 0 rows affected (0.52 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-07-21 12:34:56');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('2016-08-18 10:00:02');
Query OK, 1 row affected (0.51 sec)
mysql> insert into DemoTable values('2018-01-03 11:02:20');
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 −

+---------------------+
| AdmissionDate       |
+---------------------+
| 2019-07-21 12:34:56 |
| 2016-08-18 10:00:02 |
| 2018-01-03 11:02:20 |
+---------------------+
3 rows in set (0.00 sec)

Here is the query to get only the date −

mysql> select date_format(AdmissionDate,'%d.%m.%Y') AS Date from DemoTable;

This will produce the following output −

+------------+
| Date       |
+------------+
| 21.07.2019 |
| 18.08.2016 |
| 03.01.2018 |
+------------+
3 rows in set (0.00 sec)
raja
Published on 27-Sep-2019 06:48:42
Advertisements