Skip to content
Advertisement

How to deal with Snowflake to_date cast issue with multiple date formats in same column?

The query in snowflake,

tends to mess up the intended dates as shown below:

This is similar to Snowsql two digit century start date cast issue, but with multiple date formats in the input column.

Advertisement

Answer

You could use a coalesce with the try_to_date. The key is to specifically put a date format in the try_to_date function so that it returns null if it can’t convert a date that doesn’t match the format. When the date doesn’t match the format it’ll fall back to the next method you specify and you can continue until you covered all your different date formats. Try something like:

This returns:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement