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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Converting varchar time HHMM to 24 hour HH:MM:SS?

Author  Topic 

meef
Posting Yak Master

113 Posts

Posted - 2014-10-29 : 13:18:50
How can I do this? I have a column that is a varchar and times are stored like this:

0600
1240
0145
2335
How can I get those to HH:MM formats? I've tried this but I don't know if it's the best way, plus there are seven 0s after the MM:

convert(time,LEFT(b.status_time,2) +':'+ RIGHT(b.status_time,2))

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-10-29 : 13:30:19
convert(time(0),LEFT(b.status_time,2) +':'+ RIGHT(b.status_time,2))


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

meef
Posting Yak Master

113 Posts

Posted - 2014-10-29 : 14:15:13
Ah, thanks. Next problem is a date that is also stored as varchar, like 20141029. I need it in mm-dd-yyyy, I can get it as yyyy-mm-dd but can't get the year to go to the end.

CONVERT(date, CONVERT(varchar(8), delivered_date),112) as delivered_date


EDIT: Never mind, got it with this:

CONVERT(varchar(20), CONVERT(date, CONVERT(varchar(8), delivered_date), 112),110)as date_delivered
Go to Top of Page
   

- Advertisement -