Solved

Military time as regular time

Posted on 2009-05-18
3
507 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

762 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

12 Experts available now in Live!

Get 1:1 Help Now