![](/img/trans.png)
[英]Need to calculate difference of days between joining date and today's date in year,month and days format
[英]How to Calculate Days,Weeks,Month,Year difference from Today's Date in SQL?
我需要檢查一個進程在所選時間段內發生了多少次。 我的時間段保存為天、月、周或年格式。 示例:3 年或 4 周或 2 個月或 5 天.. 類似這樣的事情。
如何計算從今天開始的 4 周或 3 年或 2 個月或 5 天?
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
如何在程序中計算這個以檢查我的狀況?
我對此一無所知,因為我是存儲過程的新手。
參考了許多類似的問題,發現需要使用 DATEDIFF ,但不知道如何用於我的要求。
請幫助
也許這個查詢可以給你線索如何計算日期
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)
希望這可以幫到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.