如何查找两个日期之间的所有星期五和节假日
表:
hDate Holiday
17/12/2011 National Day
01/01/2012 New Year
....
从表中,我想找到两个日期之间的假期总数:
类似的查询:
select count(hdate)
from table1
where hdate between '" & start_date & "' and '" & end_date & "'
用户输入:
start_date = '16/12/2011'
end_date = '15/01/2012'
而且我想找到两个日期之间的星期五。
要查找星期五,如何创建查询?
预期输出:
Holiday Friday
2 5
[2] - 表 1 中的 2 天假期,[5] - 星期五 5 天
如何做到这一点?
The table:
hDate Holiday
17/12/2011 National Day
01/01/2012 New Year
....
From the table, i want to find the total number of holidays between two dates:
A query like:
select count(hdate)
from table1
where hdate between '" & start_date & "' and '" & end_date & "'
User input:
start_date = '16/12/2011'
end_date = '15/01/2012'
and also I want to find the friday between 2 dates.
For finding fridays, how to create a query?
Expected output:
Holiday Friday
2 5
[2] - 2 days holiday from table1, [5] - 5 days friday
How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这计算了两个日期之间的星期五:
假期很容易找到,看起来您已经涵盖了该部分。
我之前已经回答过这个问题了。但没有得到任何信用:
如何计算 TSQL 中两个日期之间的“星期二”数量?
This counts the fridays between 2 dates:
The holidays are easy to find, it seems like you have that part covered already.
I sort of answered this earlier. But didn't get any credit:
How to calculate the number of "Tuesdays" between two dates in TSQL?
该选择可以帮助您:
That select help you:
请参阅:
为什么应该我考虑使用辅助日历表?
See:
Why should I consider using an auxiliary calendar table?
对于假期,您的 SQL 看起来不错,只是您似乎在将参数插入 SQL 时遇到了麻烦。如果您指定您正在使用哪种编程语言,我们也许可以在这里提供帮助。如果您使用 .NET,则应该使用参数化查询而不是字符串替换。
对于周五,请参阅此问题:
For the holiday, your SQL looks fine, you just seem to have trouble plugging the parameters into the SQL. If you specify which programming language you are using, we might be able to help here. If you use .NET, you should use Parameterized Queries instead of string substitution.
For the fridays, see this question:
我们用额外的时间表来解决这个问题。看起来像这样
通过这个表,您可以像这样解决您的问题
这也应该与 SQL Server 2000 兼容。对于 SQL Server 2005 及以上版本:
We solving the problem with an extra time table. This looks like this
With this table you could resolve your question like this
This should also be SQL Server 2000 compatible. And this for SQL Server 2005 and above: