Calculating Previous Monday and Sunday Dates in T-SQL: An In-Depth Analysis of Date Computations and Boundary Handling

Dec 08, 2025 · Programming · 24 views · 7.8

Keywords: T-SQL | Date Calculation | GETDATE Function | Previous Week Dates | Boundary Handling

Abstract: This article provides a comprehensive exploration of methods for calculating the previous Monday and Sunday dates in SQL Server using T-SQL. By analyzing the combination of GETDATE(), DATEADD, and DATEDIFF functions, along with DATEPART for handling week start boundaries, it explains best practices in detail. The article compares different approaches, offers code examples, and discusses performance considerations to help developers efficiently manage time-related queries.

Introduction

In database applications, it is often necessary to calculate specific date ranges based on the current date, such as determining the start and end dates of the previous week. This article focuses on implementing this functionality in SQL Server using T-SQL, with particular attention to accurately calculating the previous Monday and Sunday.

Core Problem Analysis

The user requirement is clear: based on the current date (example: September 14, 2012), calculate:

  1. Previous Monday's date: September 3, 2012
  2. Previous Sunday's date: September 9, 2012

This involves two key computations: identifying the current week and then rolling back to the previous week, accurately pinpointing its Monday and Sunday.

Review of Fundamental Date Functions

SQL Server offers a rich set of date and time functions, with the following being most relevant to this discussion:

Analysis of Best Practice Solution

Based on the top-rated answer (score: 10.0) from the Q&A data, the core solution is as follows:

Basic Calculation Logic

Formula for calculating the start of last week (Monday):

SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0)

Formula for calculating the end of last week (Sunday):

SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 6)

Logical Breakdown

  1. DATEDIFF(wk, 6, GETDATE()): Calculates the number of week differences between January 6, 1900 (a Saturday) and the current date. January 6, 1900 is chosen as a reference point because it is a Saturday, helping to align week starts.
  2. DATEADD(wk, ..., 0): Adds the calculated week difference to the base date (January 1, 1900, a Monday) to obtain the Monday of the current week.
  3. By adjusting the second parameter (0 or 6), the dates for Monday or Sunday can be derived respectively.

Boundary Case Handling

The original approach has a potential issue: when the current date is a Sunday, calculations may produce incorrect results. The best answer addresses this with:

DECLARE @input varchar(10)
SET @input = GETDATE()

SELECT DATEADD(wk, DATEDIFF(wk, 6, 
CASE DATEPART(dw,@input)
WHEN 1 THEN DATEADD(d,-1,@input)
ELSE @input
END
), 0)

SELECT DATEADD(wk, DATEDIFF(wk, 6, 
CASE DATEPART(dw,@input)
WHEN 1 THEN DATEADD(d,-1,@input)
ELSE @input
END
), 6)

Key improvements here:

Comparison of Alternative Approaches

The Q&A data also includes another method (score: 2.6):

-- Get current week's Sunday
SELECT DATEADD(dd, DATEPART(DW,GETDATE())*-1+1, GETDATE())

-- Get previous week's Sunday
SELECT DATEADD(dd, DATEPART(DW,GETDATE())*-1-6, GETDATE())

This method directly calculates based on the current day of the week:

While logically feasible, this approach has limitations:

  1. Depends on SET DATEFIRST settings, as week start days may vary by region
  2. Does not explicitly handle boundary cases where Sunday is the week start
  3. Lower code readability with less intuitive calculation logic

Performance and Maintainability Considerations

In practical applications, beyond functional correctness, it is essential to consider:

Performance Optimization

Code Maintainability

Practical Application Example

Below is a complete stored procedure example for obtaining the previous week's date range:

CREATE PROCEDURE GetLastWeekDates
    @StartDate DATE OUTPUT,
    @EndDate DATE OUTPUT
AS
BEGIN
    DECLARE @CurrentDate DATE = CAST(GETDATE() AS DATE)
    DECLARE @AdjustedDate DATE
    
    -- Adjust for Sunday cases
    IF DATEPART(dw, @CurrentDate) = 1
        SET @AdjustedDate = DATEADD(DAY, -1, @CurrentDate)
    ELSE
        SET @AdjustedDate = @CurrentDate
    
    -- Calculate previous Monday
    SET @StartDate = DATEADD(WEEK, DATEDIFF(WEEK, 6, @AdjustedDate), 0)
    
    -- Calculate previous Sunday
    SET @EndDate = DATEADD(WEEK, DATEDIFF(WEEK, 6, @AdjustedDate), 6)
END

Conclusion

Calculating the previous Monday and Sunday dates is a common date-handling requirement in SQL Server. The best practice solution combines DATEDIFF, DATEADD, and DATEPART functions, ensuring accuracy and robustness through a clear reference date and boundary handling. Developers should select appropriate methods based on specific application scenarios, while also considering performance optimization and code maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.