简体   繁体   English

从Hhh:mm:ss中删除总天数

[英]Removing total days from Hhh:mm:ss

I have a varchar field that stores a duration for connections that occasionally goes into the days. 我有一个varchar字段,用于存储连接的持续时间(偶尔会持续几天)。 I'm trying to separate the total number of days from the hh:mm:ss in this field so I can store them separately but I'm getting an out of range error whenever I try to convert this data to any type of datetime variation in order to datepart the days from the value and then deduct. 我正在尝试将hh:mm:ss的总天数与该字段分开,以便我可以将它们分开存储,但是每当我尝试将此数据转换为任何类型的日期时间变化时,我都会收到超出范围的错误为了将日期从值中区分出来,然后扣除。 Is their any way of doing this or will I need to resort to a crude charindex(':' just to remove the days? 他们有什么办法做到这一点,还是我需要使用粗略的charindex(':'来删除日期?

You'll have to chop it up since it's not in a valid date or time format: 您必须将其切碎,因为它的日期或时间格式无效:

DECLARE @timeString VARCHAR(50) = '114:48:50'
SELECT   Day_CT = LEFT(@timeString ,CHARINDEX(':',@timeString )-1)/24
        ,Tm = CAST(CAST(LEFT(@timeString ,CHARINDEX(':',@timeString )-1)%24 AS VARCHAR(12))+STUFF(@timeString ,1,CHARINDEX(':',@timeString )-1,'') AS TIME)

This is one of the many reasons why dates and times shouldn't be stored in string data types, it makes using them ugly. 这是不应将日期和时间存储在字符串数据类型中的众多原因之一,这使它们使用起来很丑陋。

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

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