首页 > 解决方案 > 将年数转换为日期名称?

问题描述

所以我以年为单位计算 datediff 并获得一个数值(例如:16.166666) 将该值转换为日期名称的最佳解决方案是什么。

例如 16.166 年会变成:16 年零 1 个月?

标签: sqlsql-server

解决方案


例子

Declare @Value float= 16.166

Select Years   = floor(@Value)
      ,Months  = floor((@Value - floor(@Value)) * 12)
      ,OrAsStr = ltrim(
                 replace(
                 replace(
                 concat(' ',floor(@Value),' Years ',floor((@Value - floor(@Value)) * 12),' Months')
                 ,' 1 Years ',' 1 Year ')
                 ,' 1 Months',' 1 Month')
                 )

退货

Years   Months  OrAsStr
16      1       16 Years 1 Month

推荐阅读