简体   繁体   English

将分钟转换为小时sql server

[英]convert minute into hours sql server

I need to convert minutes into hours in sql server. 我需要在sql server中将分钟转换成小时。

I used following logic to do it. 我使用以下逻辑来做到这一点。

 CAST(REPLACE(LEFT(CONVERT(varchar(10), DATEADD(MINUTE, 19.80 *100, ''), 114),5),':','.')    AS Decimal(5,2)) AS tpschedhours

My expected Output is 33 hours (1980 minutes in hours) 我的预期输出是33小时(以小时为单位的1980分钟)

But I got output as 9 hours. 但是我得到了9个小时的输出。 I have found that, the issue occurs because DATEADD(MINUTE,1980, '') returns ouptut as 1900-01-02 09:00:00.000 (One day + 9 hours). 我发现,发生此问题是因为DATEADD(MINUTE,1980, '')返回ouptut为1900-01-02 09:00:00.000 (一天+ 9小时)。 But I need the Output as Hours value ie 33 hours 但是我需要Output as Hours值,即33 hours

Thanks for the help 谢谢您的帮助

You can try in following: 您可以尝试以下操作:

DECLARE @time INT = 1980

SELECT LEFT(CONVERT(VARCHAR(10), DATEADD(MINUTE, @time / 60 + (@time % 60), ''),114),5)
SELECT CONVERT(varchar(5), 
       DATEADD(minute, DATEDIFF(minute, @StartDate, @EndDate), 0), 114) 

I Got the solution from the answers. 我从答案中得到了解决方案。

SELECT CAST((CAST(((2.72) *100)AS INT) / 60 )+ (CAST((2.72 *100)AS INT) % 60) / 100.0 AS DECIMAL(5,2)).

thanks tinka and Stanislovas Kalašnikovas 感谢tinka和StanislovasKalašnikovas

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

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