在雪花
我想添加否。到日期领域的工作日。
以下是我的逻辑,如果一天在星期一和没有的话,这是不起作用的。天数超过5
dateadd(DAY,
(iff(dayofweek(to_date(Start_Date_Column) ) = 1, 0 ,
(TRUNCATE(((dayofweek(to_date(Start_Date_Column)) + No_DAYS - 1)/5)) * 2)) + No_DAYS) , to_date(Start_Date_Column));
。对于以下情况
select
dateadd(DAY,
(iff(dayofweek(to_date('2021-01-04') ) = 1, 0 ,
(TRUNCATE(((dayofweek(to_date('2021-01-04')) + 5 - 1)/5)) * 2)) + 5) , to_date('2021-01-04'))
I'm trying to add no. of business days to a date field.
Below is my logic, this is not working if day falls on Monday and if no. of days are more than 5.
dateadd(DAY,
(iff(dayofweek(to_date(Start_Date_Column) ) = 1, 0 ,
(TRUNCATE(((dayofweek(to_date(Start_Date_Column)) + No_DAYS - 1)/5)) * 2)) + No_DAYS) , to_date(Start_Date_Column));
e.g.. For the below scenario the date is moving to 2021-01-09(which is Saturday) instead of monday(2021-01-11)
select
dateadd(DAY,
(iff(dayofweek(to_date('2021-01-04') ) = 1, 0 ,
(TRUNCATE(((dayofweek(to_date('2021-01-04')) + 5 - 1)/5)) * 2)) + 5) , to_date('2021-01-04'))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在将特定的工作日添加到日期之后,递归CTE将获得下一个工作日。
原始表 -
列next_dy将根据特定天数(例如10)
(UDF)函数创建,包括递归CTE,用于获取下一个BIZ Day-
匿名块来调用函数和更新源表 -
运行更新后表 -
,也可以使用,它几乎没有硬编码
即乘以乘以3的天数,三天在几天内添加额外的时间
(弥补中级SAT/SUN)以获取工作日。
更新后表 -
旧 - 解决方案(无效)添加天数并搬到下周一 -
要获得下一个工作日,请检查是否添加后的第二天是星期六或阳光,如果是这样,请使用函数
next_day
与某些test-data查询:
参考href =“ https://docs.snowflake.com/en/sql-reference/functions-date time.html#supported-date-date-and-time-parts“ rel =“ nofollow noreferrer”> date noreferrer“> date time
此外,您可能需要在此处添加更多信息,因为可以改变工作日的定义。
Recursive CTE to get next business day, after adding specific number of business days to a date.
Original table -
Column next_dy to be modified with next business day, based on specific number of days (e.g. 10)
(UDF) Function creation including recursive CTE for getting next biz day -
Anonymous Block to call function and update source table -
Table after running the update -
Below can also be used, which has little-bit of hard-coding
i.e. multiply number of days with 3 to go bit over extra in days
(to compensate for intermediate sat/sun) for fetching business days.
Table after update -
Old - solution (not valid) to add days and move to next Monday -
To get next business day, check if day after adding is sat or sun and if so, get to next Monday using function
next_day
Query with some test-data:
Refer for date-time
Also, you might need to add more here as the definition of a business day can be varied.