计算两个日期之间的天数,不包括周末 [英] Count days between two dates excluding weekends

查看:312
本文介绍了计算两个日期之间的天数,不包括周末的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到DB2中两个日期之间的差额(不包括周末)?

How can I find the difference between two dates in DB2 (excluding weekends)?

在DB2中是否有任何函数可以做到这一点?还是我需要自己编写查询?

Are there any functions that will do this in DB2? Or do I need to write a query myself?

推荐答案

没有AFAIK这样的功能。但是,编写查询来计算该值很容易:

There is AFAIK no such function. It is however easy to write a query that calculates this:

with cal(d) as ( 
    values date('2015-01-01') -- start_date 
    union all 
    select d + 1 day from cal 
    where d < '2015-01-15'    -- end_date 
) select count(case when dayofweek(d) between 2 and 6 then 1 end) 
  from cal;

如果您进行了大量此类计算,则可能要创建日历表,则可以在此表中添加国定假日等属性。

If you do a lot of these kind of calculations you might want to create a calendar table, you can add attributes like national holiday etc to this table.

这篇关于计算两个日期之间的天数,不包括周末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆