SELECT
DATEDIFF(month,'2021-05-16 18:00:00',getdate())*1.0/12 as '年差值' ,
cast(DATEDIFF(month,'2021-04-16 18:00:00',getdate())*1.0/12 as decimal(18,1)) as '年差值-保留小数位',
DATEDIFF(Hour,'2022-05-16 18:00:00',getdate())*1.0/24 as '天差值' ,
cast(DATEDIFF(Hour,'2022-04-16 18:00:00',getdate())*1.0/24 as decimal(18,1)) as '天差值-保留小数位',
DATEDIFF(MINUTE,'2022-05-16 18:00:00',getdate())*1.0/60 as '小时差值' ,
cast(DATEDIFF(MINUTE,'2022-05-16 18:00:00',getdate())*1.0/60 as decimal(18,1)) as '小时差值-保留小数位'

本文介绍了一种使用SQL来计算不同日期之间的年份、天数及小时差值的方法,并展示了如何通过不同的时间单位来获取更精确的时间间隔。

5331

被折叠的 条评论
为什么被折叠?



