本文介绍了在SQL中从iso周和年获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从iso周和年开始,我想得到一个日期.日期应该是一周的第一天.一周的第一天是星期一.例如iso周10和iso年应转换为2019-03-04.我正在使用雪花

From iso week and year, I would like to get a date.The date should be first day of the week. First day of the week is Monday.For example iso week 10 and iso year should convert to 2019-03-04.I am using Snowflake

推荐答案

执行此操作的日期表达式有点复杂,但并非不可能:

The date expression to do this is a little complex, but not impossible:

SELECT
    DATEADD( /* Calculate start of ISOWeek as offset from Jan 1st */
      DAY,
      WEEK * 7 - CASE WHEN DAYOFWEEKISO(DATE_FROM_PARTS(YEAR, 1, 1)) < 5 THEN 7 ELSE 0 END
      + 1 - DAYOFWEEKISO(DATE_FROM_PARTS(YEAR, 1, 1)),
      DATE_FROM_PARTS(YEAR, 1, 1)
    )
FROM (VALUES (2000, 1), (2000, 2), (2001, 1), (2002, 1), (2003, 1)) v(YEAR, WEEK);

这篇关于在SQL中从iso周和年获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 02:45