Syntax error MySQL strip time component from datetime?

MySQL strip time component from datetime?



You can strip time component from datetime with the help of DATE() function. The syntax is as follows −

SELECT DATE(yourColumnName) from yourTableName;

To understand the above concept, let us first create a table −

mysql> create table StripComponentDatetimeDemo
   -> (
   -> YourDateTime datetime
   -> );
Query OK, 0 rows affected (0.60 sec)

Let us now insert some records in the table. The query is as follows −

mysql> insert into StripComponentDatetimeDemo values(date_add(now(),interval 1 day));
Query OK, 1 row affected (0.13 sec)

Display records with the help of select statement. The query is as follows displaying datetime −

mysql> select *from StripComponentDatetimeDemo;

The following is the output −

+---------------------+
| YourDateTime        |
+---------------------+
| 2018-11-25 20:15:07 |
+---------------------+
1 row in set (0.00 sec)

The following is the query that strips time component −

mysql> SELECT DATE(YourDateTime) from StripComponentDatetimeDemo;

The following is the output −

+--------------------+
| DATE(YourDateTime) |
+--------------------+
| 2018-11-25         |
+--------------------+
1 row in set (0.00 sec)
Updated on: 2019-07-30T22:30:24+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements