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
 How to convert military time to standard time

Author  Topic 

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-03-06 : 15:28:53
How to convert military time to standard time

15:12:47 to 2:12:47 PM?

9:13:33 to 9:13:33 AM?

lmnorms

8 Posts

Posted - 2009-03-06 : 15:38:14
Hello,
Military time is also called 24 hour time. From 00:00:01 to 24:00:00.
If the time is less than 12:00:00 just use clock time. But from 12:59:59 forward the time should take the clock time and add 12 hours. For 1:00 pm it would convert to 13:00 hrs. To get the time from 'Military' time subtract 12 hours. 14:12:47 is 2:12:47pm.

Hope the information helps.
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-03-06 : 15:40:47
do u have any script i will test?
Go to Top of Page

lmnorms

8 Posts

Posted - 2009-03-06 : 15:41:52
Actually, I think I have a vb script has an example if that is ok.
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-03-06 : 15:42:55
let me try
Go to Top of Page

lmnorms

8 Posts

Posted - 2009-03-06 : 15:47:18
Here are some functions. If you need something else let me know.

' convert to a string in military time
Dim input As String = DateTime.Now.ToString(militaryTimeFormat)

' convert from a string in military time
Dim time As DateTime = DateTime.ParseExact(input,
militaryTimeFormat, Nothing)
Go to Top of Page

lmnorms

8 Posts

Posted - 2009-03-06 : 15:47:47
Here are some functions. If you need something else let me know.

' convert to a string in military time
Dim input As String = DateTime.Now.ToString(militaryTimeFormat)

' convert from a string in military time
Dim time As DateTime = DateTime.ParseExact(input,
militaryTimeFormat, Nothing)
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-03-06 : 15:50:44
i don't think so its gonna implement on sql script?
ParseExact is your own function??
Go to Top of Page

lmnorms

8 Posts

Posted - 2009-03-06 : 15:53:57
No. These are vb functions. You should have access to similar sql functions that retrieve time. Tell me a little more about the situation. How, and why do you need to convert?
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-03-06 : 16:07:01
it does not implement on T-SQL.
guys any body have idea?
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-06 : 16:08:05
IS it a datetime field that you are trying to convert?
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-03-06 : 16:10:05
yes its datetime field.
i don't need date. Just need to convert time.
Like this:
15:12:47 to 3:12:47 PM?
9:13:33 to 9:13:33 AM?

Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-03-06 : 16:33:45
vijayisonly, did u get?
quote:
Originally posted by vijayisonly

IS it a datetime field that you are trying to convert?

Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-06 : 16:36:28
Try this...

SELECT LTRIM(RIGHT(CONVERT(char,urcol,109),18)) from urtable
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-03-06 : 16:43:05
thanks vijayisonly. good job
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-03-06 : 16:43:09
109 will return you milliseconds as well. Try this,

select right(convert(varchar, getdate(),0),7)
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-03-06 : 16:52:30
Yes its return milliseconds too. I don't need milliseconds.

I tried this but get err
SELECT LTRIM(RIGHT(CONVERT(char,mycol,09),7)) from mytbl
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-03-06 : 16:59:30
[code]SELECT right(convert(varchar, mycol,0),7) from mytbl[/code]
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-03-06 : 17:13:28
how to get seconds
quote:
Originally posted by sakets_2000

SELECT  right(convert(varchar, mycol,0),7) from mytbl


Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-06 : 17:58:47
Do this...

SELECT REPLACE(LTRIM(RIGHT(CONVERT(char,urcol,109),18)),':000',' ') from urtable
Go to Top of Page
   

- Advertisement -