choloboy
choloboy

Reputation: 815

Date Conversion To Year-Month In Snowflake

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.

Upvotes: 10

Views: 38810

Answers (2)

Lukasz Szozda
Lukasz Szozda

Reputation: 175726

The equivalent of FORMAT is TO_VARCHAR:

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

Upvotes: 16

Nat Taylor
Nat Taylor

Reputation: 1108

TO_DATE() does this, like TO_DATE(date_column, 'yyyy-MM') See https://docs.snowflake.com/en/sql-reference/functions/to_date.html

Upvotes: 2

Related Questions