T-SQL格式秒为HH:MM:SS时间 [英] T-SQL Format seconds as HH:MM:SS time
本文介绍了T-SQL格式秒为HH:MM:SS时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何棘手的方式来格式化秒,例如小时:分钟:秒.例如,
Is there any tricky way to format seconds like hours:minutes:seconds. For example,
3660
3660
秒将显示为
01h 01m 00s
01h 01m 00s
或
01:01:00
01:01:00
我知道这样做的标准方法:
I am aware of the standard way of doing this:
- 在3600上分配所有秒数以获取小时数
- 将剩下的秒数除以60可得到分钟数
- 剩下的就是几秒钟
我遇到了以下问题:
-
我无法创建执行此操作的单独功能.
I am not able to create separate function that do this.
正在使用几个CTE查看我的代码.因此,可以声明变量 仅使用CTE.
My code is in view using several CTEs. So, variables can be declare using the CTEs only.
推荐答案
SELECT CONVERT(VARCHAR(10),Seconds/3600)
+':'
+ RIGHT('00'+CONVERT(VARCHAR(2),(Seconds%3600)/60),2)
+':'
+ RIGHT('00'+CONVERT(VARCHAR(2),Seconds%60),2) AS [HH:MM:SS]
FROM table1
这篇关于T-SQL格式秒为HH:MM:SS时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文