我會建議選擇在可變冒號第一的指標,然後您運行基於該字符串的解析。下面是我在SSMS中使用的原始代碼:
declare @fecha varchar(30)
set @fecha='9:4:2'
select
cast(substring(@fecha, 1, HourLimiter - 1) as INT)
, cast(substring(@fecha, HourLimiter + 1, MinuteLimiter - HourLimiter - 1) as INT)
, cast(substring(@fecha, MinuteLimiter + 1, len(@fecha) - MinuteLimiter) as INT)
from (
select
charindex(':', @fecha) AS HourLimiter
, charindex(':', @fecha, charindex(':', @fecha)+1) AS MinuteLimiter
) t
set @fecha='29:59:59'
select
cast(substring(@fecha, 1, HourLimiter - 1) as INT)
, cast(substring(@fecha, HourLimiter + 1, MinuteLimiter - HourLimiter - 1) as INT)
, cast(substring(@fecha, MinuteLimiter + 1, len(@fecha) - MinuteLimiter) as INT)
from (
select
charindex(':', @fecha) AS HourLimiter
, charindex(':', @fecha, charindex(':', @fecha)+1) AS MinuteLimiter
) t
我也有SQLFiddle基於表的解決方案。
請說明:29真的是一個有效的小時?傳入的字符串是否以幾小時的時間結束? –
是的,這是因爲我會使用datediff(小時,29,'1900-01-01') – angel