Skip to content

Instantly share code, notes, and snippets.

@r1d3rzz
Created July 3, 2024 07:44
Show Gist options
  • Save r1d3rzz/24b3a23810158bf7d826e94d834e716f to your computer and use it in GitHub Desktop.
Save r1d3rzz/24b3a23810158bf7d826e94d834e716f to your computer and use it in GitHub Desktop.
Get Previous StartDate And EndDate
DECLARE @CurrentDate DATE = GETDATE();
DECLARE @FirstDayOfPreviousMonth DATE;
DECLARE @LastDayOfPreviousMonth DATE;
-- Calculate the first day of the previous month
SET @FirstDayOfPreviousMonth = DATEADD(MONTH, DATEDIFF(MONTH, 0, @CurrentDate) - 1, 0);
-- Calculate the last day of the previous month
SET @LastDayOfPreviousMonth = EOMONTH(DATEADD(MONTH, -1, @CurrentDate));
-- Select the results
SELECT @FirstDayOfPreviousMonth AS StartDate, @LastDayOfPreviousMonth AS EndDate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment