sql - 将年数转换为日期名称?
问题描述
所以我以年为单位计算 datediff 并获得一个数值(例如:16.166666) 将该值转换为日期名称的最佳解决方案是什么。
例如 16.166 年会变成:16 年零 1 个月?
解决方案
例子
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
推荐阅读
- excel - VBA - 按最接近的值对列进行排序以回答。最接近的答案将出现在排序列表的顶部
- powershell - PowerShell 方法包含未按预期工作
- r - 是否可以在 R 的匿名函数中命名变量?
- c - GCC 预处理器不工作?有或没有注释的大文件的编译时间
- html - 以百分比计算 getBoundingClientRect
- typescript - 有没有一种简单的方法来对打字稿字典进行排序?
- reactjs - Jest 中“beforeEach”全局的目的是什么?
- r - gganimate:具有静态线的状态之间的转换
- apache - 如何下载 perl 文件而不是运行它
- excel - 激活事件被 PasteSpecial 操作触发