用sql求出兩個年份差(不足年按年計算)

declare @date1 varchar(10),@date2 varchar(10)
set @date1='2019-10-10'
set @date2='2020-10-11'

select case when month(@date2)>month(@date1) then DATEDIFF(YEAR,@date1,@date2)+1
                   when month(@date2)=month(@date1) and Day(@date2)>DAY(@date1) then  DATEDIFF(YEAR,@date1,@date2)+1
                   else DATEDIFF(YEAR,@date1,@date2) end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章