January 27, 2010 at 9:48 am
Need to update/convert a varchar column to time.
Example
varcharTimeColumn
-------------------
00.13.00
Trying to update another column I created.
timeColumn
------------
00.13.00
update table
set timeColumn = varcharTimeColumn
keep getting the error:
Conversion failed when converting date and/or time from character string.
January 27, 2010 at 9:56 am
Try CASTING the value to time to be sure it is in the proper format.
January 27, 2010 at 9:59 am
I tried this:
update dbo.VerizonDetails
set totalTime2 = cast(totalTime as time)
but got the errror:
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
January 27, 2010 at 10:09 am
tan110 (1/27/2010)
I tried this:update dbo.VerizonDetails
set totalTime2 = cast(totalTime as time)
but got the errror:
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
Try:
update dbo.VerizonDetails
set totalTime2 = cast(REPLACE(totalTime, '.', ':') as time)
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
January 27, 2010 at 10:15 am
I went ahead and replaced the '.' with ':', and reran the update statement:
update dbo.VerizonDetails
set totalTime2 = totalTime
still getting error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
January 27, 2010 at 10:16 am
Also tried this:
update dbo.VerizonDetails
set totalTime2 = cast(totalTime as time)
but getting error message:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
January 27, 2010 at 10:36 am
Curious, the following test code worked just fine on my system:
declare @ TimeVal varchar(16); -- Added space between @ and TimeVal to get code to post
set @TimeVal = '00.13.00';
select @TimeVal, cast(replace(@TimeVal,'.',':') as time)
January 27, 2010 at 10:47 am
its weird, when I run:
declare @ TimeVal varchar(16); -- Added space between @ and TimeVal to get code to post
set @TimeVal = '00.13.00';
select @TimeVal, cast(replace(@TimeVal,'.',':') as time)
declare @ TimeVal varchar(16); -- Added space between @ and TimeVal to get code to post
set @TimeVal = '00:13:00.0';
select @TimeVal, cast(@TimeVal as time)
Both scripts above work fine.
But when running in an update statement:
update dbo.VerizonDetails
set totalTime2 = cast(totalTime as time)
still getting error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
January 27, 2010 at 10:50 am
tan110 (1/27/2010)
its weird, when I run:declare @ TimeVal varchar(16); -- Added space between @ and TimeVal to get code to post
set @TimeVal = '00.13.00';
select @TimeVal, cast(replace(@TimeVal,'.',':') as time)
declare @ TimeVal varchar(16); -- Added space between @ and TimeVal to get code to post
set @TimeVal = '00:13:00.0';
select @TimeVal, cast(@TimeVal as time)
Both scripts above work fine.
But when running in an update statement:
update dbo.VerizonDetails
set totalTime2 = cast(totalTime as time)
still getting error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Please show results of this:
select top 5 totalTime from dbo.VerizonDetails;
January 27, 2010 at 10:56 am
00:13:00
00:01:00
00:03:00
00:09:00
00:01:00
January 27, 2010 at 10:57 am
Thanks everyone, was able to get it working with set based statement. Had to loop through all the records.
January 27, 2010 at 11:00 am
tan110 (1/27/2010)
Thanks everyone, was able to get it working with set based statement. Had to loop through all the records.
Care to share?
January 27, 2010 at 3:23 pm
tan110 (1/27/2010)
Had to loop through all the records.
Heh... GAHHH!!!! 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
January 27, 2010 at 10:03 pm
tan110 (1/27/2010)
Thanks everyone, was able to get it working with set based statement. Had to loop through all the records.
Would you care to elaborate on how a set-based method is looping through all the records? Seems like an oxy-moron to me...
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 28, 2010 at 12:55 pm
tan110 (1/27/2010)
its weird, when I run:declare @ TimeVal varchar(16); -- Added space between @ and TimeVal to get code to post
set @TimeVal = '00.13.00';
select @TimeVal, cast(replace(@TimeVal,'.',':') as time)
declare @ TimeVal varchar(16); -- Added space between @ and TimeVal to get code to post
set @TimeVal = '00:13:00.0';
select @TimeVal, cast(@TimeVal as time)
Both scripts above work fine.
Sure, first script uses "." which becomes a ":" due to replacement. Second script directly uses ":", so there is no replacement required.
BTW
Please share your solution (and you don't need a loop 😉 )
Greets
Flo
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply