简体   繁体   English

日期转换为雪花中的年月

[英]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.我正在尝试将日期(格式为 yyyy-mm-dd)转换为年月格式(例如:2021-07),同时保留日期数据类型。

Coming from SQL Server, this could be done using the FORMAT function like this:来自 SQL Server,这可以使用FORMAT函数来完成,如下所示:

SELECT FORMAT(date_column, 'yyyy-MM')

I am wondering how this could be achieved in SNOWFLAKE .我想知道如何在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.我在SNOWFLAKE 中找不到FORMAT函数,所以我尝试使用 DATE、DATE_PART,但它们似乎都使我的列成为 varchar,丢失了它的日期类型。

Thanks.谢谢。

FORMAT的等价物是TO_VARCHAR

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

TO_DATE()这样做,就像TO_DATE(date_column, 'yyyy-MM')https://docs.snowflake.com/en/sql-reference/functions/to_date.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM