Convert Decimal Time Value to Military Time Value

Hello – I have a SQL 2008 R2 Database that stores a Start Time as a Decimal (4,0) value.  The output appears as:  700, 1100, 1330 etc.

I want to display the time as 0700 AM, 1100 AM, 1330 PM etc.

I attempted to convert the data type but run into all sorts of errors.  I have found several posts regarding converting a time value to decimals but not the other way around.  

Since I am going to use this information in a report, I would prefer to create a SQL view that will contact the data fields I need.  I need help with the time conversion please.  The Date field is a DateTime field, so that is less painful.  

SQLwatcherExpert Technical Support ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott CraigWebmasterCommented:
So, just to confirm, the data is stored as 13.50 for 13:30 and you want it displayed as 13:30 PM, correct?
This will give you a DateTime with today's date and the time component specified.  The ToString("0000") is simply padding with leading zeros.

      static void Main( string[] args )
         decimal timeAsDecimal = 700.0m;
         DateTime parsedTime = DateTime.ParseExact( timeAsDecimal.ToString( "0000" ), "hhmm", null );
         Console.WriteLine( parsedTime );

Open in new window

SQLwatcherExpert Technical Support ConsultantAuthor Commented:
Hi ...well actually the date does not have a decimal in it however the field type in SQL is decimal (4,0).
The code you provided is JAVA, correct?  I may not be able to conver that in a SQL View.?  
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

This is a function I use with int values.  should work fine with DECIMAL(4,0)

CREATE FUNCTION [dbo].[IntToTime] (@TimeValue int)
RETURNS datetime AS  
Declare @Hour char(2)
Declare @Min char(2)

Set @Hour = substring(cast(100 + Cast ((@TimeValue / 100) as int) as char(3)),2,2)
set @Min = substring(cast(100 + Cast ((@TimeValue % 100) as int) as char(3)),2,2)

Return ( cast(@Hour + ':' + @min  as datetime))
Anthony PerkinsCommented:
Something like this perhaps:
SELECT RIGHT(CONVERT(varchar(20), DATEADD(minute, @YourDecimalValue / 100 + @YourDecimalValue % 100, '19000101'), 0), 7)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SQLwatcherExpert Technical Support ConsultantAuthor Commented:
Thank you for the help.  Sorry for getting back to you so late.  :o(
SQLwatcherExpert Technical Support ConsultantAuthor Commented:
Thank You!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.