
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL Query to convert from datetime to date?
You can use CAST() function from MySQL to achieve this. 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. The query to create a table is as follows −
mysql> create table ConvertDateTimeToDate -> ( -> ArrivalDatetime datetime -> ); Query OK, 0 rows affected (0.37 sec)
Insert the datetime in the table using insert command. The query is as follows −
mysql> insert into ConvertDateTimeToDate values(date_add(now(),interval -1 year)); Query OK, 1 row affected (0.19 sec) mysql> insert into ConvertDateTimeToDate values('2017-11-21 13:10:20'); Query OK, 1 row affected (0.14 sec) mysql> insert into ConvertDateTimeToDate values('2016-05-24 21:11:24'); Query OK, 1 row affected (0.26 sec) mysql> insert into ConvertDateTimeToDate values('2012-04-30 04:05:50'); Query OK, 1 row affected (0.13 sec)
Let us now display all records from the table using select command. The query is as follows.
mysql> select *from ConvertDateTimeToDate;
The following is the output.
+---------------------+ | ArrivalDatetime | +---------------------+ | 2017-12-27 10:05:21 | | 2017-11-21 13:10:20 | | 2016-05-24 21:11:24 | | 2012-04-30 04:05:50 | +---------------------+ 4 rows in set (0.00 sec)
Here is the query to convert from datetime to date in MySQL.
mysql> select cast(ArrivalDatetime as Date) as Date from ConvertDateTimeToDate;
The following is the output.
+------------+ | Date | +------------+ | 2017-12-27 | | 2017-11-21 | | 2016-05-24 | | 2012-04-30 | +------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to convert JS date time to MySQL datetime?
- MySQL query to get current datetime and only current date
- How to select only MySQL date from datetime column?
- How to convert JavaScript datetime to MySQL datetime?
- MySQL query to get only the minutes from datetime?
- MySQL query to select date from timestamp?
- How to convert date to datetime in Python?
- How to part DATE and TIME from DATETIME in MySQL?
- Convert from varchar to datetime and compare in MySQL?
- MySQL query to select date from 00:00 to today’s date
- MySQL query to select closest date from today?
- Convert INT to DATETIME in MySQL?
- Create DATETIME from DATE and TIME in MySQL?
- Get only the date from datetime in MySQL?
- How to convert date YYYYMMDD to YY-MM-DD in MySQL query?

Advertisements