如何计算“星期二”的数量在TSQL中的两个日期之间? [英] How to calculate the number of "Tuesdays" between two dates in TSQL?

查看:95
本文介绍了如何计算“星期二”的数量在TSQL中的两个日期之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



p星期二可以是任何值。

我想知道如何计算TSQL中两个日期之间的星期二数量? p>

解决方案

  declare @from datetime ='9/20/2011'
declare @ to datetime ='9/28/2011'

select datediff(day,-6,@to)/ 7-datediff(day,-5,@from)/ 7




  1. 在@from之前的星期二星期一查找星期一。

  2. 在@to

  3. 之后的星期一查找星期


I'm trying to figure out how to calculate the number of "Tuesdays" between two dates in TSQL?

"Tuesday"could be any value.

解决方案

declare @from datetime= '9/20/2011' 
declare @to datetime  = '9/28/2011' 

select datediff(day, -6, @to)/7-datediff(day, -5, @from)/7

  1. find the week of the first monday before the tuesday in @from.
  2. find the week of the first monday after @to
  3. subtract the weeks

这篇关于如何计算“星期二”的数量在TSQL中的两个日期之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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