SQL 返回两个传入日期之间的工作日数

SQL to return the number of working days between 2 passed in dates(SQL 返回两个传入日期之间的工作日数)
本文介绍了SQL 返回两个传入日期之间的工作日数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个 sql 查询,返回两个给定日期之间的工作日(周一至周五)数.

I need to write an sql query that returns the number of Working days (Monday - Friday) between two given dates.

我想知道最有效的方法是什么?

I was wondering what would be the most efficient way to do this?

SELECT           --Start with total number of days including weekends             
(DATEDIFF(dd,@StartDate,@EndDate)+1) --Subtact 2 days for each full weekend 
(DATEDIFF(wk,@StartDate,@EndDate)*2) --If StartDate is a Sunday, Subtract 1          
ELSE 0               END)            --If EndDate is a Saturday, Subtract 1 
FROM dual

然后,能够从该计数中删除假期(例如圣诞节和节礼日)也会很有帮助.

Then it would also be helpful to be able to remove holidays from this count such as christmas day and boxing day.

有什么想法吗?

推荐答案

计算两个日期之间的工作日数的简单方法是:

an easy way to calculate to number of weekdays between 2 dates is :

SELECT
date1,
date2,
((date2-date1)-2*FLOOR((date2-date1)/7)-DECODE(SIGN(TO_CHAR(date2,'D')-
    TO_CHAR(date1,'D')),-1,2,0)+DECODE(TO_CHAR(date1,'D'),7,1,0)-
    DECODE(TO_CHAR(date2,'D'),7,1,0))*24 as WorkDays
FROM
  tablename
ORDER BY date1,date2

这篇关于SQL 返回两个传入日期之间的工作日数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)