JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Converting from datetime type to date-only in MySQL

Author:JIYIK Last Updated:2025/04/23 Views:

Today, we will learn the DATE(), CAST(), CONVERT() and DATE_FORMAT() methods to convert DATETIME type to DATE type in MySQL. The above mentioned methods can be used in MySQL 4.0 and above.


Converting from DATETIME to DATE in MySQL

To understand it through code examples, let us create an order table with id, productName, orderNumber, and orderDateTime as attributes; we will also create a primary key using the id field. See the following query.

Sample code:

CREATE TABLE orders(
    id INT NOT NULL,
    productName VARCHAR(50) NOT NULL,
    orderNumber INT NOT NULL,
    orderDateTime DATETIME NOT NULL,
    PRIMARY KEY(id)
);

INSERT INTO orders (id, productName, orderNumber, orderDateTime)
VALUES
(1, 'Oppo F17', 3322, '2022-04-11 04:32:15'),
(2, 'DELL Laptop', 5433, '2022-05-12 12:23:09'),
(3, 'HP Mouse', 3489, '2022-05-16 07:23:16');

SELECT * FROM orders;

Output:

+----+-------------+-------------+---------------------+
| id | productName | orderNumber | orderDateTime       |
+----+-------------+-------------+---------------------+
|  1 | Oppo F17    |        3322 | 2022-04-11 04:32:15 |
|  2 | DELL Laptop |        5433 | 2022-05-12 12:23:09 |
|  3 | HP Mouse    |        3489 | 2022-05-16 07:23:16 |
+----+-------------+-------------+---------------------+
3 rows in set (0.00 sec)

Once the table is ready, we can now use any of the following methods to meet the objectives as per the project requirements.


Convert DATETIME to DATE using DATE() in MySQL

Sample code:

SELECT id, productName, orderNumber, DATE(orderDateTime) FROM orders;

Output:

+----+-------------+-------------+---------------------+
| id | productName | orderNumber | DATE(orderDateTime) |
+----+-------------+-------------+---------------------+
|  1 | Oppo F17    |        3322 | 2022-04-11          |
|  2 | DELL Laptop |        5433 | 2022-05-12          |
|  3 | HP Mouse    |        3489 | 2022-05-16          |
+----+-------------+-------------+---------------------+
3 rows in set (0.00 sec)

DATE()The function can extract the DATE part from a valid DATE or DATETIME type field. If an invalid expression is passed, it will return NULL.

If we also want to group the data based on DATE only, we can use this post.


Convert DATETIME to DATE using CAST() in MySQL

Sample code:

SELECT id, productName, orderNumber, CAST(orderDateTime AS DATE) FROM orders;

Output:

+----+-------------+-------------+-----------------------------+
| id | productName | orderNumber | CAST(orderDateTime AS DATE) |
+----+-------------+-------------+-----------------------------+
|  1 | Oppo F17    |        3322 | 2022-04-11                  |
|  2 | DELL Laptop |        5433 | 2022-05-12                  |
|  3 | HP Mouse    |        3489 | 2022-05-16                  |
+----+-------------+-------------+-----------------------------+
3 rows in set (0.00 sec)

CAST()Methods are used to convert (cast) a value of one data type to another given data type.


Convert DATETIME to DATE using CONVERT() in MySQL

Sample code:

SELECT id, productName, orderNumber, CONVERT(orderDateTime, DATE) FROM orders;

Output:

+----+-------------+-------------+------------------------------+
| id | productName | orderNumber | CONVERT(orderDateTime, DATE) |
+----+-------------+-------------+------------------------------+
|  1 | Oppo F17    |        3322 | 2022-04-11                   |
|  2 | DELL Laptop |        5433 | 2022-05-12                   |
|  3 | HP Mouse    |        3489 | 2022-05-16                   |
+----+-------------+-------------+------------------------------+
3 rows in set (0.00 sec)

We can also CONVERT()convert a DATETIME type field to just DATE using the method. It accepts two parameters - the first is the value that needs to be converted and the second is the data type into which the given value will be converted.


Convert DATETIME to DATE using DATE_FORMAT() in MySQL

Sample code:

SELECT id, productName, orderNumber,
DATE_FORMAT(orderDateTime, '%Y-%m-%d') FROM orders;

Output:

+----+-------------+-------------+----------------------------------------+
| id | productName | orderNumber | DATE_FORMAT(orderDateTime, '%Y-%m-%d') |
+----+-------------+-------------+----------------------------------------+
|  1 | Oppo F17    |        3322 | 2022-04-11                             |
|  2 | DELL Laptop |        5433 | 2022-05-12                             |
|  3 | HP Mouse    |        3489 | 2022-05-16                             |
+----+-------------+-------------+----------------------------------------+
3 rows in set (0.00 sec)

DATE_FORMAT()The function converts DATETIME to DATE type and is useful for changing the current date format. For example, if we want to display DATE in d/m/Y format, we can do it as follows.

Sample code:

SELECT id, productName, orderNumber,
DATE_FORMAT(orderDateTime, '%d/%m/%Y') FROM orders;

Output:

+----+-------------+-------------+----------------------------------------+
| id | productName | orderNumber | DATE_FORMAT(orderDateTime, '%d/%m/%Y') |
+----+-------------+-------------+----------------------------------------+
|  1 | Oppo F17    |        3322 | 11/04/2022                             |
|  2 | DELL Laptop |        5433 | 12/05/2022                             |
|  3 | HP Mouse    |        3489 | 16/05/2022                             |
+----+-------------+-------------+----------------------------------------+
3 rows in set (0.00 sec)

If we want to see the name of the month instead of the number of the month, we can also use DATE_FORMAT()the function to convert it to the following format.

Sample code:

SELECT id, productName, orderNumber,
DATE_FORMAT(orderDateTime, '%D %M %Y') FROM orders;

Output:

+----+-------------+-------------+----------------------------------------+
| id | productName | orderNumber | DATE_FORMAT(orderDateTime, '%D %M %Y') |
+----+-------------+-------------+----------------------------------------+
|  1 | Oppo F17    |        3322 | 11th April 2022                        |
|  2 | DELL Laptop |        5433 | 12th May 2022                          |
|  3 | HP Mouse    |        3489 | 16th May 2022                          |
+----+-------------+-------------+----------------------------------------+
3 rows in set (0.00 sec)

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Changing max_allowed_packet Size in MySQL Server

Publish Date:2025/04/22 Views:193 Category:MySQL

This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi

Zerofill usage, advantages and alternatives in MySQL

Publish Date:2025/04/22 Views:196 Category:MySQL

In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input

Compare only MySQL timestamp dates to date parameters

Publish Date:2025/04/22 Views:64 Category:MySQL

In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi

Calculating Percentages in MySQL

Publish Date:2025/04/22 Views:67 Category:MySQL

We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re

Selecting multiple values using WHERE in MySQL

Publish Date:2025/04/22 Views:186 Category:MySQL

This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi

Changing the connection timeout in MySQL

Publish Date:2025/04/22 Views:59 Category:MySQL

We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti

MySQL fix Data Is Truncated for a Column error

Publish Date:2025/04/22 Views:102 Category:MySQL

This article describes possible causes and solutions for the MySQL error Data is truncated for a column . Fix data truncated due to column error in MySQL Here, we will discuss the possible causes and solutions to eliminate MySQL data trunca

MySQL Error Server PID File Could Not Be Found Solution

Publish Date:2025/04/22 Views:193 Category:MySQL

In this article, we will study about the Error! Error Server PID File Could Not Be Found! in MySQL and its solution with full explanation. MySQL PID file The file that contains the process identification number or process ID of a running My

Get the last inserted ID using PHP MySQLi function

Publish Date:2025/04/22 Views:100 Category:MySQL

This article briefly introduces the PHP mysqli() function and demonstrates how to use it to get the last inserted ID from a MySQL database. PHP mysqli() Function It is an extended version of the MySQL driver called mysqli and is typically u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial