简体   繁体   English

以 HHH:MM:SS 格式 (Nvarchar) 将天转换为小时

[英]Convert days to hours in HHH:MM:SS Format (Nvarchar)

I have a table where i need to convert for example 2.6 days or 5 days, 3.2 days etc to hours.我有一张表,我需要将例如 2.6 天或 5 天、3.2 天等转换为小时。 Since it is above 24 hours format i know i need to convert to nvarchar.由于它是 24 小时以上格式,我知道我需要转换为 nvarchar。 I searched many forums and i cannot see exactly how can i do specifically this, i think i have to convert this days to seconds and then from seconds to nvarchar format.我搜索了许多论坛,但我无法确切地看到我该如何具体做到这一点,我想我必须将这一天转换为秒,然后从秒转换为 nvarchar 格式。 does anyone have any idea?有人有什么主意吗?

Thx all:)谢谢大家:)

You seem to think you need to use nvarchar.您似乎认为您需要使用 nvarchar。 But that is not necessarily the case.但事实并非如此。 If you can store your hours as decimal.如果您可以将小时数存储为十进制。 The solution is simple解决方法很简单

hours = days * 24

If you want to break it down to hours minutes and seconds you need to convert it to seconds.如果要将其分解为小时分钟和秒,则需要将其转换为秒。 There are 24 hours in a day, 60 minutes in an hour and 60 seconds in a minute.一天有24小时,一小时有60分钟,一分钟有60秒。

seconds = days * 24 * 60 * 60 

Once you have the seconds you can use the division operator (/) togheter with the modulo operator (%).获得秒数后,您可以将除法运算符 (/) 与模运算符 (%) 一起使用。 If you store your days as decimal (3,1) you will not have to worry about seconds, because the resolution is too low.如果您将天数存储为十进制 (3,1),则不必担心秒数,因为分辨率太低。

select 
 days
 ,CAST(days * 24 * 60 * 60 as int) as totalseconds --days converted to full seconds
 ,CAST(days * 24 * 60 as int) as totalminutes --days converted to full minutes
 ,CAST(days * 24  as int) as totalfullhours --days converted to full hours
 ,CAST(days * 24 * 60 as int) % 60 as remainingminutes -- since the row above contains full hours, we need to find out how many minutes are in the split hours f ex 0.2 hours
 ,CAST(days * 24 * 60 * 60 as int) % 60 as remainingseconds -- this will always be 0 because of your precision
from YourTable

If you still want the result as a single string column (HH::mm) it will will be如果您仍希望将结果作为单个字符串列 (HH::mm),它将是

select 
     CAST(CAST(days * 24  as int) as varchar(10)) + ':' + RIGHT('0'+CAST(CAST(days * 24 * 60 as int) % 60 as varchar(10)),2) 
    from YourTable

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

相关问题 如何在mssql中将NVARCHAR值转换为dd-mm-yyyy hh:mm:ss AM / PM格式 - How to convert NVARCHAR value to dd-mm-yyyy hh:mm:ss AM/PM format in mssql 从sql中的天数转换为hh:mm:ss - convert into hh:mm:ss from days in sql MySQL:如何将秒转换为mm:ss格式? - MySQL: How to convert seconds to mm:ss format? 将小时转换为天:SQL Server中的小时格式 - Convert hours into days:Hours format in SQL Server 如何在 SQL 中将小时、分钟和秒 (HH:mm:ss) 转换为分钟和秒 (mm:ss) - How to Convert Hours, minutes and seconds (HH:mm:ss) to minutes and seconds (mm:ss) in SQL 将格式为HH:MM AM / PM的时间转换为HH:MM:SS - Convert time with format HH:MM AM/PM to HH:MM:SS 将时间hh:mm:ss转换为十进制值(小时或分钟) - Convert time hh:mm:ss to decimal values (hours or minutes) 如何将 YYMMDDhhmm 格式的 NVARCHAR 转换为 YYYY-MM-DD hh:mm:ss 格式的 DATETIME? - How to convert YYMMDDhhmm formatted NVARCHAR into YYYY-MM-DD hh:mm:ss formatted DATETIME? 尝试以(yyyy-mm-dd hh:mm:ss)日期格式转换字符串格式(dd-mm-yyyy hh:mm:ss) - Trying to convert string format (dd-mm-yyyy hh:mm:ss) in (yyyy-mm-dd hh:mm:ss) date format 如何将 nvarchar(255) 转换为 YYYY-MM-DD 格式? - How to convert a nvarchar(255) to YYYY-MM-DD format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM