

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to cast DATETIME as a DATE in MySQL?
To cast DATETIME as a DATE in MySQL, use the CAST() function. The syntax is as follows −
select cast(yourColumnName as Date) as anyVariableName from yourTableName;
To understand the above syntax, let us first create a table −
mysql> create table ConvertDatetimeToDate −> ( −> YourDatetime datetime −> ); Query OK, 0 rows affected (0.95 sec)
Inserting datetime into the table with the help of insert command. The query is as follows −
mysql> insert into ConvertDatetimeToDate values(date_add(now(),interval 1 day)); Query OK, 1 row affected (0.17 sec) mysql> insert into ConvertDatetimeToDate values(date_add(now(),interval -1 day)); Query OK, 1 row affected (0.15 sec) mysql> insert into ConvertDatetimeToDate values(date_add(now(),interval 1 year)); Query OK, 1 row affected (0.10 sec) mysql> insert into ConvertDatetimeToDate values(date_add(now(),interval -1 year)); Query OK, 1 row affected (0.16 sec)
Display all the records inserted above. The query is as follows −
mysql> select *from ConvertDatetimeToDate;
The following is the output −
+---------------------+ | YourDatetime | +---------------------+ | 2018-11-28 16:12:50 | | 2018-11-26 16:13:00 | | 2019-11-27 16:13:13 | | 2017-11-27 16:13:24 | +---------------------+ 4 rows in set (0.00 sec)
Now you can apply the above syntax to cast datetime to date. The query is as follows −
mysql> select cast(YourDatetime as Date) as OnlyDateDemo from ConvertDatetimeToDate;
The following is the output −
+--------------+ | OnlyDateDemo | +--------------+ | 2018-11-28 | | 2018-11-26 | | 2019-11-27 | | 2017-11-27 | +--------------+ 4 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL CAST as DATE?
- How to convert JS date time to MySQL datetime?
- How to update date of datetime field with MySQL?
- How to select only MySQL date from datetime column?
- How to part DATE and TIME from DATETIME in MySQL?
- How to check if a datetime value equals tomorrows date in MySQL?
- MySQL Query to convert from datetime to date?
- Insert current date in datetime format MySQL?
- MySQL GROUP BY date when using datetime?
- Create DATETIME from DATE and TIME in MySQL?
- Get only the date from datetime in MySQL?
- How to convert date to datetime in Python?
- How to set default date time as system date time in MySQL?
- How to compare DateTime Column with only Date not time in MySQL?
- How to set NOW() as default value for datetime datatype in MySQL?
Advertisements