[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 686
  • Last Modified:

SQL get military time from string time - SQL Server 2005

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
robthomas09
Asked:
robthomas09
  • 2
  • 2
  • 2
  • +1
3 Solutions
 
SharathData EngineerCommented:
What is the data type of time column?
0
 
robthomas09Author Commented:
it is varchar(255) - thanks!
0
 
SharathData EngineerCommented:
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
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.

 
Kevin CrossChief Technology OfficerCommented:
Try this:
select REPLACE(CONVERT(char(5), CONVERT(datetime, appt_time), 8), ':', '')
from appointments;

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
Sorry, Sharath.  I started typing my post before I saw yours.
0
 
MaximumIQCommented:
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
 
MaximumIQCommented:
^ Late as always
0
 
robthomas09Author Commented:
Thanks!
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now