1

Problem

I need to query out a date value for use in some ETL processing. However, the data is stored in MySQL as a VARCHAR column in values like 1/1/19.

What I've tried

A simple CAST(myDateColumn as DATE) fails as I get values like 0001-01-19 returned back.

Question

Am I able to pass a custom date format string into the CAST call somehow to tell it how to parse out the date parts? If not, could a SUBSTRING type of function help here?

4
  • 3
    Have you read the docs on the STR_TO_DATE() function? Commented Apr 27, 2019 at 17:35
  • Why not fix your data set? Commented Apr 27, 2019 at 17:36
  • See also stackoverflow.com/questions/47861550/… Commented Apr 27, 2019 at 17:38
  • Dataset isn't mine to fix, unfortunately. @BillKarwin That STR_TO_DATE() looks like the ticket! Commented Apr 27, 2019 at 17:38

1 Answer 1

2

As discussed in the comments above, you can use the STR_TO_DATE() function to parse a string into a proper YYYY-MM-DD date.

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

1 Comment

For this example specifically, if someone needs it, it ended up being STR_TO_DATE(myDateColumn, '%m/%d/%Y').

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.