Convert date string to insert in mysql date format

I have a bunch of txt files that contains a lot of data, including a date in format

dd-mm-YYYY

for example this date

15-03-2014

Researching around here i found a way to convert this date to insert in database in the correct date format YYY-mm-dd

the query is

STR_TO_DATE('$array[12]', '%Y-%m-%d')

But i'm having weird results like

2019-07-20

How can i correctly insert them to database any tips of what i'm doing wrong?

Upvotes: 1

Views: 2170

Answers (1)

John Conde
John Conde

Reputation: 219804

The second parameter should be in the format your date is stores, not the format you're looking for which is always YYYY-MM-DD. So:

STR_TO_DATE('$array[12]', '%Y-%m-%d')

should be:

STR_TO_DATE('$array[12]', '%d-%m-%Y')

Upvotes: 4

Related Questions