[英]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.