简体   繁体   English

将时间浮点数转换为格式 HH:mm sql server

[英]Convert time float to format HH:mm sql server

I need to format a float decimal number into a time format hour:minute.我需要将浮点十进制数格式化为时间格式小时:分钟。

I wrote this Scalar-Value Functions with an input float and output varchar(6):我用输入浮点数和输出 varchar(6) 编写了这个标量值函数:

CREATE FUNCTIONE formatOre ( @input float )
returns varchar(6) 
as
begin
declare @n float;
declare @hour int = floor(@input);
declare @minutes int = (select (@input - floor(@input)) * 60);
declare @val varchar(6)

set @val = right('00' + convert(varchar(2), @hour), 2) + ':' + right('00' + convert(varchar(2), @minutes), 2);

return @val

end

It looks like great, but not for everything records.它看起来很棒,但不是所有记录。 This is my output:这是我的输出:

select formatOre (0)    ---> 00:00
select formatOre (0.17) ---> 00:10
select formatOre (0.25) ---> 00:15
select formatOre (0.33) ---> 00:19
select formatOre (0.42) ---> 00:25
select formatOre (0.5)  ---> 00:30
select formatOre (0.58) ---> 00:34
select formatOre (0.67) ---> 00:40
select formatOre (0.75) ---> 00:45
select formatOre (0.83) ---> 00:49
select formatOre (0.92) ---> 00:55

As you can see from the results, there are 3 wrongs conversion: 0.33 = 00:19 // 0.58 = 00:34 // 0.83 = 00:49 .从结果中可以看出,有 3 个错误转换: 0.33 = 00:19 // 0.58 = 00:34 // 0.83 = 00:49

How do I set the correct output?如何设置正确的输出?

Use function FORMAT SQL 2012+ https://docs.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql使用函数 FORMAT SQL 2012+ https://docs.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql

DECLARE  @input float = 4.92    
SELECT FORMAT(FLOOR(@input)*100 + (@input-FLOOR(@input))*60,'00:00')

Not a fix to the problem but perhaps something you can utilize.不是解决问题的方法,而是您可以利用的东西。 Scalar functions are horrible for performance.标量函数对于性能来说是可怕的。 But inline table valued functions are not.但内联表值函数不是。 The function you posted can be very easily converted to an inline table valued function.您发布的函数可以很容易地转换为内联表值函数。 Keep in mind that these must be ONLY a single select statement.请记住,这些必须只是一个单一的选择语句。 If you have variables and such it become a multi-statement table valued function and the performance would be even worse than a scalar function.如果你有变量等它变成一个多语句表值函数,性能会比标量函数更差。

Here is how you could convert that scalar into an inline table valued function.以下是如何将该标量转换为内联表值函数。

CREATE FUNCTION formatOre 
(
    @input float 
)
returns table as return

select right('00' + convert(varchar(2), floor(@input)), 2) + ':' + right('00' + convert(varchar(2), floor((@input - floor(@input)) * 60)), 2)

You can Try this你可以试试这个

 SELECT FORMAT(FLOOR(ColumnNAme) + (ColumnNAme -FLOOR(ColumnNAme)),'00.00') from TableName

输出:

将 4.92 渲染为“04:55”:

SELECT FORMAT(DATEADD(minute, 4.92 * 60, '2000-01-01'), 'HH:mm')
INSERT INTO t_att_inout(MM  ,YYYY   ,   DATE,EmpCode    ,Type   ,InTime,    OutTime)
select 10 as MM,2020 as YYYY, CONVERT(DATE,LEFT([Att Date],2)+'-OCT-2020',106),
emp_code,'fboth'as type,  
CONVERT(TIME(7),CONVERT(VARCHAR,FLOOR([in time]))+':'+RIGHT('00'+CONVERT(VARCHAR,CONVERT(INT,([In Time]-FLOOR([In Time]))*100)),2)),  
CONVERT(TIME(7),CONVERT(VARCHAR,FLOOR([out time]))+':'+RIGHT('00'+CONVERT(VARCHAR,CONVERT(INT,([Out time]-FLOOR([Out time]))*100)),2)) --,[IN time],[out time]
from CGAttend10$    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM