Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL get military time from string time - SQL Server 2005

Posted on 2011-03-23
8
Medium Priority
?
683 Views
Last Modified: 2012-06-22
Hello experts,

I have a time field that I need to convert into military 4-digit 4-char varchar.  I currently have:

table: appointments
time:
03:20PM
03:30PM
03:40PM
03:50PM
08:00AM
08:10AM
08:20AM
08:30AM

and I would like to land on:

table: appointments
time:
1320
1330
1340
1350
0:00
0810
0820
0830

Thoughts?

Thanks!
0
Comment
Question by:robthomas09
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 35202066
What is the data type of time column?
0
 

Author Comment

by:robthomas09
ID: 35202091
it is varchar(255) - thanks!
0
 
LVL 41

Accepted Solution

by:
Sharath earned 1000 total points
ID: 35202104
If the data type is varchar,try this.
select replace(left(convert(varchar,CONVERT(datetime,[time]),108),5),':','') as TimePart from appointments

Open in new window

If the data type is datetime, try this.
select replace(left(convert(varchar,[time],108),5),':','') as TimePart from appointments

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 600 total points
ID: 35202132
Try this:
select REPLACE(CONVERT(char(5), CONVERT(datetime, appt_time), 8), ':', '')
from appointments;

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 35202139
Sorry, Sharath.  I started typing my post before I saw yours.
0
 
LVL 4

Assisted Solution

by:MaximumIQ
MaximumIQ earned 400 total points
ID: 35202203
If you're trying to do this directly in SQL I wouldn't know. But you can always use a VB Script or a simple VB solution to pull the fields, convert them and overwrite the table. The actual conversion in VB would look something like this.  

Dim strTime As String
Dim intSeperator As Integer

For Each strTime In TimeList
    intSeperator = InStr(Item, ":")
    
    Select Case UCase(Right(strTime, 2))
        Case Is = "AM"
            strNewTime = Replace(Replace(strTime, ":", ""), "AM", "")
        Case Is = "PM"
            strNewTime = Int(Left(strTime, intsperator - 1)) + 12 & Mid(strTime, intSeperator, 2)
        Case Else
            strNewTime = strTime
    End Select
Next Item

Open in new window

0
 
LVL 4

Expert Comment

by:MaximumIQ
ID: 35202213
^ Late as always
0
 

Author Closing Comment

by:robthomas09
ID: 35202282
Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question