2

I have dates in the format '6/30/2020'. It is a string and I want to convert it into date format.

List of methods I have tried

Cast('6/30/2020' as date) #returns null
to_date('6/30/2020','yyyy/MM/dd') #returns null

I also tried splitting the string and then concatenating it into data. After trying all this and putting all the possible combinations in the to_date function, I am still getting the answer as null.

Now I am confused as I have used all the functions to convert string to date. Thanks in advance for your help.

1 Answer 1

3

The date format you used was incorrect. Try this:

select to_date('6/30/2020', 'M/dd/yyyy')

If you want to format your result, you can use date_format:

select date_format(to_date('6/30/2020', 'M/dd/yyyy'), 'yyyy/MM/dd')

Note that to_date converts a given string from the given format, while date_format converts a given date to the given format.

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

3 Comments

How it is working for 12/30/2020 when the specified date format is 'M/dd/yyyy' and why 6/30/2020 is not working with 'MM/dd/yyyy' when 12/30/2020 is.
because if you specify MM, you need the month to have 2 digits. But 6 only has one digit, so the format is incorrect. Note that the number of M's means the minimum number of digits.
@BrownBatman see the docs for more details on date string formatting.

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.