Calculating the Number of Tuesdays Between Two Dates in TSQL
When working with temporal data in TSQL, it can be useful to calculate the number of instances of a specific weekday between two dates. To determine the number of "Tuesdays" within a specified date range, consider the following solution:
To get the count of Tuesdays between two dates, you can use the following formula:
<code class="sql">datediff(day, -6, @to)/7-datediff(day, -5, @from)/7</code>
In this formula:
Example Usage:
To calculate the number of Tuesdays between March 1, 2013, and March 31, 2013:
<code class="sql">declare @from datetime= '3/1/2013' declare @to datetime = '3/31/2013' select datediff(day, -6, @to)/7-datediff(day, -5, @from)/7 AS TUE</code>
Output:
TUE: 5
This query would return 5, indicating that there are five Tuesdays within the specified date range.
The above is the detailed content of How many Tuesdays are there between two dates in TSQL?. For more information, please follow other related articles on the PHP Chinese website!