
- 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 CAST as DATE?
There is no difference between cast as Date and date() function in MySQL.
The syntax of both cast() and date() is as follows −
cast(yourDateTimeColumnName as Date) date(yourDateTimeColumnName)
Both functions internally call Item_date_typecast. To check both the functions, let us create a table. The query to create a table is as follows −
mysql> create table CastAsDateDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> ArrivalTime datetime, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.71 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into CastAsDateDemo(ArrivalTime) values('2014-1-13 13 −45 −46'); Query OK, 1 row affected (0.16 sec) mysql> insert into CastAsDateDemo(ArrivalTime) values('2016-4-11 10 −30 −31'); Query OK, 1 row affected (0.48 sec) mysql> insert into CastAsDateDemo(ArrivalTime) values('2019-1-9 19 −17 −49'); Query OK, 1 row affected (0.13 sec) mysql> insert into CastAsDateDemo(ArrivalTime) values(date_add(now(),interval 2 day)); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from CastAsDateDemo;
The following is the output −
+----+----------------------+ | Id | ArrivalTime | +----+----------------------+ | 1 |2014-01-13 13 −45 −46 | | 2 |2016-04-11 10 −30 −31 | | 3 |2019-01-09 19 −17 −49 | | 4 |2019-01-11 19 −17 −59 | +----+---------------------+ 4 rows in set (0.00 sec)
Here is the query to cast as a date using cast() −
mysql> select cast(ArrivalTime as Date) as only_Date from CastAsDateDemo;
The following is the output −
+------------+ | only_Date | +------------+ | 2014-01-13 | | 2016-04-11 | | 2019-01-09 | | 2019-01-11 | +------------+ 4 rows in set (0.00 sec)
Here is the query to cast as a date using date(). The query is as follows −
mysql> select date(ArrivalTime) as only_Date from CastAsDateDemo;
The following is the output −
+------------+ | only_Date | +------------+ | 2014-01-13 | | 2016-04-11 | | 2019-01-09 | | 2019-01-11 | +------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to cast DATETIME as a DATE in MySQL?
- MySQL - CAST DECIMAL to INT?
- Select timestamp as date string in MySQL?
- Sorting a VARCHAR column as FLOAT using the CAST operator isn’t working in MySQL ?
- How Can MySQL CAST handle overflow?
- How to set default date time as system date time in MySQL?
- Cast string to date in specific format in SAP HANA
- How to get string as date in MySQL with dates as dot format specifier?
- How is it possible to store date such as February 30 in a MySQL date column?
- Creating a Table in MySQL to set current date as default
- How to cast from VARCHAR to INT in MySQL?
- Convert UK DATE to MySQL date?
- How can I store ‘0000-00-00’ as a date in MySQL?
- Which punctuation character can be used as the delimiter between MySQL date parts?
- How do I cast a type to a BigInt in MySQL?

Advertisements