Hi,@Ranjit Srivastva
Continue to complement the discussion above . According to the explanation of the official documentation , if datepart is a month , and the number of days in the date-month is more than the number of days in the returned month , and the date-of-day does not exist in the returned month , the last day of the returned month is returned.
If you want to get the last day of the previous month, you can do something like this:
DECLARE @date DATETIME = '2022-03-31'
SELECT
CASE WHEN DATEDIFF(MONTH, @date, DATEADD(DAY, 1, @date)) = 1 THEN
DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, @date), 0)) ELSE DATEADD(MONTH, -1, @date)end
return result:
select DATEADD(MONTH,1,'2022-02-28')
The operation here is to find a date within a month from the start date , so ‘2022-02-28’ will find the corresponding March record , but the function will not provide a value for the end of the month.
For more questions about DateADD, please refer to this link.
Best regards,
Bert Zhou
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.