Date Conversion To Year-Month In Snowflake

Viewed 5926

I am attempting to convert a date (formatted as yyyy-mm-dd) to a year-month format (ex: 2021-07) while keeping the date datatype.

Coming from SQL Server, this could be done using the FORMAT function like this:

SELECT FORMAT(date_column, 'yyyy-MM')

I am wondering how this could be achieved in SNOWFLAKE. I couldn't find the FORMAT function in SNOWFLAKE so I attempted with DATE, DATE_PART, but they all seem to make my column a varchar, losing its date type.

Thanks.

2 Answers

The equivalent of FORMAT is TO_VARCHAR:

TO_VARCHAR( <date_or_time_expr> [, '<format>' ] )
SELECT TO_VARCHAR(date_column, 'yyyy-MM')
Related