How to calculate the number of “Tuesdays” between two dates in TSQL?

前端 未结 4 1672
[愿得一人]
[愿得一人] 2020-11-29 12:29

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

\"Tuesday\"could be any value.

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 12:57

    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

提交回复
热议问题