Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


convert military time to standard time


convert military time to standard time

Author
Message
wlblee38
wlblee38
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 915
need a way (SQL) to convert military time to a standard time format. I almost got it but I'm having a little trouble.

1338= 01:38:00 PM
1112= 11:12:00 AM
1135= 11:35:00 AM
1149= 11:49:00 PM
2014= 08:14:00 PM
22:35= 10:35:00 PM

Here's what I have so far; any help would do

select mycol, Right(Convert(VarChar(30),
Convert(DateTime, Stuff(Stuff(Right(Replicate('0', 6) + Convert(VarChar(8), mycol), 6), 3,0, ':'), 6, 0, ':')), 100), 7) as timevalues from miltarytbl
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5695 Visits: 7660
I think it's because of where you're including the replicated 0's.

This will work:


DECLARE @MilDate VARCHAR(20)

SET @MilDate = '13:38'

SET @MilDAte = REPLACE( @MilDate, ':', '')
SELECT @MilDate

SET @MilDate = LEFT( @MilDate + REPLICATE( '0', 6), 6)
SELECT @MilDate AS Timevalues

SET @MilDate = STUFF( STUFF( @MilDate, 3, 0, ':'), 6, 0, ':')
SELECT @MilDate

SELECT CONVERT( VARCHAR(25), CONVERT( DATETIME, @MilDATE), 100)



EDIT: I lied, close, but jumped the gun. Formatting is off when I apply it to convert to style 100. Back in a minute or two.

Heh, beat myself up with a typo, whoops. All's good.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
wlblee38
wlblee38
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 915
I'm not that familiar with functions is there a way to do this with SQL? Here's what i revise from your example.

select mycol, LEFT( mycol+ REPLICATE( '0', 6), 6)+STUFF( STUFF( mycol, 3, 0, ':'), 6, 0, ':')+ CONVERT( VARCHAR(25), CONVERT( TIME, mycol), 100)from militarytbl
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5695 Visits: 7660
wlblee38 (1/7/2011)
I'm not that familiar with functions is there a way to do this with SQL? Here's what i revise from your example.


Ermm That was SQL. Proc based, but SQL. If you're talking about inlineSQL for a table statement...

select mycol, LEFT( mycol+ REPLICATE( '0', 6), 6)+STUFF( STUFF( mycol, 3, 0, ':'), 6, 0, ':')+ CONVERT( VARCHAR(25), CONVERT( TIME, mycol), 100)from militarytbl



This will have some trouble running, primarily because you broke the layering. I broke out each step above to help you see how each piece modified the data. I'll get back to you about the final, all in one, it'll take some manipulation to combine properly... and if you're using the TIME datatype as I see above, are you on SQL 2k8 or are you actually on SQL 2k?


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search