What is the method for converting datetime to date in MySQL?

In MySQL, you can use either the CAST() function or the STR_TO_DATE() function to convert between datetime and date.

  1. Convert datetime to date.
SELECT CAST(datetime_column AS DATE) AS date_column
FROM table_name;
  1. Convert the date to datetime.
SELECT CAST(date_column AS DATETIME) AS datetime_column
FROM table_name;

Alternatively, you can use the STR_TO_DATE() function.

SELECT STR_TO_DATE(date_column, '%Y-%m-%d %H:%i:%s') AS datetime_column
FROM table_name;

In the example above, datetime_column is the name of the column that stores both date and time, date_column is the name of the column that stores only the date, and table_name is the name of the data table. These names can be replaced with the actual column and table names.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds