Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Convert hhmmss to datetime
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Yak Posting Veteran

72 Posts

Posted - 03/21/2013 :  12:18:03  Show Profile  Reply with Quote

I have a 3rd party database that I am converting to our datebase and am trying to find the easiest/best way to convert some data.

The source data has the date in char(8) as yyyymmdd and a simple CAST(DATE AS DATETIME) AS OURDATE worked as needed.

With their time field, it isn't working as smoothly... That is in char(6) and it is hhmmss. Should I just plug in a default date and then convert it to datetime? I have tried the following code but it is erroring out...

cast((date+' '+time_rcvd) as datetime) as TRCVD

Slow But Sure Yak Herding Master

United Kingdom
3608 Posts

Posted - 03/21/2013 :  12:41:22  Show Profile  Reply with Quote
You are probably going to have to substring it.

CAST((Date + ' ' + SUBSTRING(time_rcvd,1,2) + ':' + SUBSTRING(time_rcvd,3,2) + ':' + SUBSTRING(time_rcvd,5,2)) AS DATETIME)
Go to Top of Page

Flowing Fount of Yak Knowledge

4614 Posts

Posted - 03/21/2013 :  12:47:40  Show Profile  Reply with Quote
If is somthing you need to do alot, I'd suggest either:
1. Making a function to do the conversion
2. Or create a table you can join to or do a lookup by the char(6) value.
Go to Top of Page

Yak Posting Veteran

72 Posts

Posted - 03/21/2013 :  13:06:40  Show Profile  Reply with Quote
Gotcha, thanks RickD... worked perfectly.

@Lamprey No, it is just going to be a one time thing. I am pulling out all of the data out of a clients legacy system and putting it into our database so am fighting with some crappy database design haha.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000