SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

cstokes91
Yak Posting Veteran

USA
59 Posts

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

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

RickD
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

Lamprey
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

cstokes91
Yak Posting Veteran

USA
59 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  
 New 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.05 seconds. Powered By: Snitz Forums 2000