'How to Calculate Days,Weeks,Month,Year difference from Today's Date in SQL?

I need to check one process occurred how many times in the selected time period. My time period saved as in Days,Months,Weeks or Year format. Example: 3 Years or 4 Weeks or 2 months or 5 days.. something like this.

How to calculate 4 weeks or 3 years or 2 months or 5 days from today's date?

Which means : Today is 23 May 2022  
>> 4 Weeks past of today is 22 April 2022
>> 3 Years past of today is 23 May 2019
>> 2 Months past of today is 23 March 2022
>> 5 Days past of today is 18 May 2019

How to calculate this in procedure to check my condition?

I got no idea in this ,as I am new to stored procedure.

Referred many similar questions, found out need to use DATEDIFF , but dont know how to use for my requirement.

Kindly help



Solution 1:[1]

Maybe this query can give you clue how to calculate date

select
'today is '+convert(varchar,getdate(),106),
'4 weeks past of today is '+convert(varchar,DATEADD(WEEK,-4,getdate()),106),
'3 years past of today is '+convert(varchar,DATEADD(YEAR,-3,getdate()),106),
'2 months past of today is '+convert(varchar,DATEADD(MONTH,-2,getdate()),106),
'5 days past of today is '+convert(varchar,DATEADD(DAY,-5,getdate()),106)

hope this can help you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kebis