Solved

SQL get military time from string time - SQL Server 2005

Posted on 2011-03-23
8
649 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 250 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
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 
LVL 59

Assisted Solution

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

Open in new window

0
 
LVL 59

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 100 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Requesting help with creating an SQL query with 2 tables 6 31
SQL Recursion schedule 13 34
query error in former passthrough query 2 31
SSMS Imprt data from Excel 7 20
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 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