該0000:00:00.00
= HHHH:MM:SS:NN
DECLARE @t table (
blah varchar(20)
);
INSERT INTO @t (blah)
VALUES ('0:00:00.00')
, ('0000:00:00.00')
, ('0000:01:37.61');
SELECT blah
, hours
, minutes
, seconds
, milliseconds
, DateAdd(ms, milliseconds, DateAdd(ss, seconds, DateAdd(mi, minutes, DateAdd(hh, hours, 0))))
FROM (
SELECT blah
, Cast(left_part As int) As hours
, Cast(SubString(right_part, 1, 2) As int) As minutes
, Cast(SubString(right_part, 4, 2) As int) As seconds
, Cast(Left(SubString(right_part, 7, 3) + '000', 3) As int) As milliseconds -- padded to 3 characters (trailing zeroes)
FROM (
SELECT blah
, SubString(blah, 0, CharIndex(':', blah)) As left_part
, SubString(blah, CharIndex(':', blah) + 1, 20) As right_part -- FYI 20 = length of varchar field
FROM (
SELECT blah
, CharIndex(':', blah) + 1 As colon
FROM @t
) As find_the_first_colon
) As split_the_string_by_first_colon
) As work_out_component_parts;
什麼錯誤你得到了嗎? –
你可以舉一個你在列中獲取的數據的例子,或者它只是0000:00:00.00? –
請參閱本 http://stackoverflow.com/questions/7674101/sql-server-datetime-null-value 問候 Ashutosh說阿里亞 –