Solved

Military time as regular time

Posted on 2009-05-18
3
509 Views
Last Modified: 2012-05-07
I have a table I'm able to select on only
It has 2 columns and an id
the columns are in military time...but are varchar(200)

I need to do a select and output as regular time with a space and AM or PM

Example

ckin
1600

needs to be output at 4:00 PM
0
Comment
Question by:lrbrister
  • 3
3 Comments
 
LVL 15

Expert Comment

by:MNelson831
ID: 24416536
Declare @Datafield as varchar(50)

Set @DataField = '1600'

select
      Case
            when left(@DataField,2) - 12 < 0 then Left(@DataField,2)
            Else Left(@DataField,2)
      End + ':' + right(@DataField,2)
0
 
LVL 15

Expert Comment

by:MNelson831
ID: 24416546
OOps

Try this instead

Declare @Datafield as varchar(50)

Set @DataField = '1600'

select
      Case
            when left(@DataField,2) - 12 < 0 then Left(@DataField,2)
            Else Left(@DataField,2) -12
      End + ':' + right(@DataField,2)
0
 
LVL 15

Accepted Solution

by:
MNelson831 earned 500 total points
ID: 24416571
Guess I should read slower... missed the AM/PM part


Declare @Datafield as varchar(50)

Set @DataField = '1600'

select
      Case
            when left(@DataField,2) - 12 < 0 then Left(@DataField,2)  + ':' + right(@DataField,2) + ' PM'
            Else Left(@DataField,2) -12 + ':' + right(@DataField,2) + ' AM'
      End as NewTimeValue
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

932 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now