如何在纯sql中计算工作日数

时间:2019-06-11 10:00:34

标签: sql oracle

我正在pl-SQL环境中工作,但访问权限非常有限。

  1. 我可以查询数据库(sql语句)
  2. 我无法创建函数或过程。

对于具有2个flds的表:

  1. startPeriod
  2. endPeriod

我要计算工作日/工作日

(例如:endPeriod-startPeriod -nonWorkingDays)

Utsav的回答很有帮助:

Want a SQL to get days between 2 date and exclude Saturday and Sunday in Oracle

但是我不能用表中的动态日期替换硬编码日期。

以下基于Utsav答案的查询返回错误消息:“无效标识符”

SELECT t,startPeriod,t.endPeriod,
   (
        With test_data as (select t.startPeriod as start_date,t.endPeriod as end_date from dual ),
        all_dates as (select td.start_date + level-1 
        as week_day from test_data td connect by 
        td.start_date + level-1< td.end_date)
        select count(*) from all_dates where 
        to_char(week_day,'dy', 'nls_date_language-american') not in ('sun','sat') and week_day not in ('01jan2019','02feb2019')
   ) as workingDaysField
FROM dbo.TBL t

0 个答案:

没有答案