Home > Database > Mysql Tutorial > How many Tuesdays are there between two dates in TSQL?

How many Tuesdays are there between two dates in TSQL?

Patricia Arquette
Release: 2024-10-29 20:50:03
Original
236 people have browsed it

How many Tuesdays are there between two dates in TSQL?

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>
Copy after login

In this formula:

  • @from and @to are the start and end dates of the range.
  • -6 and -5 represent the day integers for Monday (-6) and Tuesday (-5), respectively.

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>
Copy after login

Output:

TUE: 5
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template