February 7, 2017 at 8:29 am
I receive duration data that is in hh:mm:ss format and stored in nvarchar(50) column
I want convert that to datetime by adding '01/01/1900' to the time part.
Tsql when I use convert(datetime, '01/01/1900 ' + [duration_hh:mm:ss]) , I get error
Conversion failed when converting date and/or time from character string.
February 7, 2017 at 8:46 am
if you convert time directly to datetime, itends up being 1900 anyway, so just do a direct conversion.
CONVERT(datetime,convert(time,durationvalue)) As DurationTime2 or directly like convert(datetime,durationvalue) AS DurationTime3WITH mySampleData(durationvalue)
AS
(
SELECT CONVERT(nvarchar(50),'00:44:21') UNION ALL
SELECT '16:54:47' UNION ALL
SELECT '16:54:49' UNION ALL
SELECT '16:54:53' UNION ALL
SELECT '18:35:11' UNION ALL
SELECT '16:02:17' UNION ALL
SELECT '15:08:32' UNION ALL
SELECT '16:54:51' UNION ALL
SELECT '00:44:21' UNION ALL
SELECT '16:54:47'
)
SELECT convert(time,durationvalue) As DurationTime,
CONVERT(datetime,convert(time,durationvalue)) As DurationTime2,
convert(datetime,durationvalue) AS DurationTime3
FROM mySampleData
Lowell
February 7, 2017 at 9:06 am
mandavli - Tuesday, February 7, 2017 8:29 AMI receive duration data that is in hh:mm:ss format and stored in nvarchar(50) column
I want convert that to datetime by adding '01/01/1900' to the time part.
Tsql when I use convert(datetime, '01/01/1900 ' + [duration_hh:mm:ss]) , I get errorConversion failed when converting date and/or time from character string.
Would you have values greater than 23:59:59? If that's possible then you might need to add a converted value. If not, then you can simply convert to datetime without any concatenation or other intermediate steps as shown by Lowell.
February 7, 2017 at 9:14 am
Luis Cazares - Tuesday, February 7, 2017 9:06 AMmandavli - Tuesday, February 7, 2017 8:29 AMI receive duration data that is in hh:mm:ss format and stored in nvarchar(50) column
I want convert that to datetime by adding '01/01/1900' to the time part.
Tsql when I use convert(datetime, '01/01/1900 ' + [duration_hh:mm:ss]) , I get errorConversion failed when converting date and/or time from character string.
Would you have values greater than 23:59:59? If that's possible then you might need to add a converted value. If not, then you can simply convert to datetime without any concatenation or other intermediate steps as shown by Lowell.
good point! i did not consider a duration more than 24 hours; i would certainly expect to sometimes see days:hours:minutes:seconds as a duration!
Lowell
February 7, 2017 at 9:20 am
I have to ask why someone is storing something that will only ever contain digits and colons as NVARCHAR. It should be stored simply as CHAR(8) if the times never exceed 24 hours.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 7, 2017 at 9:47 am
Jeff Moden - Tuesday, February 7, 2017 9:20 AMI have to ask why someone is storing something that will only ever contain digits and colons as NVARCHAR. It should be stored simply as CHAR(8) if the times never exceed 24 hours.
It should be stored as time.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply