1

I am importing a CSV file I download from a website to display to clients in my PHP App. The date the website spits out in the CSV is not MySQL friendly. I want to convert it in the table immediately after importing the CSV file. The date in the CSV file is this format: February 06 2023 08:26:44

I'm running the following query:

update tblname set `time` = STR_TO_DATE(`time`, '%Y-%m-%d %H:%i:%s') where `time` = STR_TO_DATE(`time`, '%M %d %Y %H:%i:%s');

This is the error I get in phpmyadmin:

MySQL said: Documentation

#1292 - Truncated incorrect datetime value: 'February 06 2023 08:26:44'

Due to the nature of the log, I need the time to be kept as well. Any help much appreciated!

6
  • 1
    You tell that "The date in the CSV file" - but use UPDATE which means that this date is already imported (incorrectly imported!) into the table. So we'd know all details of this skipped step. Commented Feb 8, 2023 at 20:05
  • You can't change the format of a DATETIME column. You do formatting when retrieving dates, not when storing them. Commented Feb 8, 2023 at 20:06
  • set time = STR_TO_DATE(time, '%Y-%m-%d %H:%i:%s') - This doesn't make sense to me. You're trying to update the time column to a formatted/parsed version of... itself? Was this "February 06 2023 08:26:44" value already imported into the DB? What is the type of the time column? What value does it contain when executing this query? Commented Feb 8, 2023 at 20:06
  • If the column is DATETIME datatype, you have to call STR_TO_DATE() when importing from the CSV. Commented Feb 8, 2023 at 20:07
  • Hi David. The CSV file has the date as 'February 06 2023 08:26:4'. The column currently has a VARCHAR value. It's a huge log that I'm importing and need to query later based on event time. Commented Feb 8, 2023 at 20:08

1 Answer 1

3

STR_TO_DATE(time, '%M %d %Y %H:%i:%s') parses the formatted date and returns the date that you want to store in the table:

UPDATE tblname 
SET `time` = STR_TO_DATE(`time`, '%M %d %Y %H:%i:%s');

The WHERE clause is not needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.