In this tutorial, we will learn how to convert date from ISO 8601 format and save to MySQL column.
Convert date from ISO 8601 format and save to MySQL column
To convert a date from ISO 8601 format and save it to a MySQL column, you can use the STR_TO_DATE()
function in MySQL. Assuming your ISO 8601 date is in the format 'YYYY-MM-DDTHH:MM:SS', you can convert and insert it into a MySQL column with the DATETIME
type.
Here's an example SQL query:
INSERT INTO your_table_name (your_datetime_column)
VALUES (STR_TO_DATE('2024-02-10T12:30:45', '%Y-%m-%dT%H:%i:%s'));
Make sure to replace your_table_name
with the actual name of your table, and your_datetime_column
with the name of the column where you want to store the date.
In the above example, '%Y-%m-%dT%H:%i:%s
' is the format specifier for the ISO 8601 date format. It tells MySQL how to interpret the date string.
Alternatively, if you're working with an existing table and need to update a row, you can use the UPDATE
statement:
UPDATE your_table_name
SET your_datetime_column = STR_TO_DATE('2024-02-10T12:30:45', '%Y-%m-%dT%H:%i:%s')
WHERE your_condition; -- Add a condition to specify which row(s) to update
Adjust the query based on your specific table structure and requirements.