sql - SQL Server 中的分钟数

标签 sql sql-server time

我创建了一个将分钟 (smallint) 转换为时间 (varchar(5)) 的函数,例如 58 -> 00:58。

set QUOTED_IDENTIFIER ON

GO
Create FUNCTION [dbo].[IntToMinutes]
(
    @m smallint
)
RETURNS nvarchar(5)
AS
BEGIN
    DECLARE @c nvarchar(5)
     SET @c = CAST((@m / 60) as varchar(2)) + ':' + CAST((@m % 60) as varchar(2))
     RETURN @c
END

问题是时间少于 10 分钟,比如 9。这个函数的结果是 0:9。我希望格式为 00:09。

我该怎么做?

最佳答案

Create FUNCTION [dbo].[IntToMinutes]
(
    @m smallint 
)  
RETURNS nvarchar(5)
AS  
BEGIN
    DECLARE @c datetime
    select @c = dateadd(mi,@m,'00:00')       
    RETURN convert(nvarchar(5), @c, 108)   
END

关于sql - SQL Server 中的分钟数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2944580/

相关文章:

mysql - 以原始形式将数据保存在数据库中,并在json中获取结果

sql - SQL Server 中的不精确列是什么?

sql - 获取两个日期之间每个月的开始和结束时间

c# - 存储过程没有参数并且提供了参数

python - 问题拆分日期时间 - 'str' 对象没有属性 'strptime'

java - 结合 OrientDB 中的其他属性查询 embeddedlist

mysql - 在数据库中拆分表是否有助于解决性能问题?

sql-server - SQL Server : 'contains' vs 'charindex'

php - 如何从 PHP 将当前时间输入到 mysql 表中

java - 如何确定时间复杂度是 O(m + n) 还是 O(Math.max(m, n))