Solved

Convert Time stored in milliseconds integer to hh:mm datetime SQL Server

Posted on 2010-09-17
10
2,636 Views
Last Modified: 2012-05-10
In a table of scheduled jobs, the JOB_START_TIME is mysteriously stored in milliseconds counting from midnight as data type integer.

I need help to convert these values to hh:mm datetime format so I can make a user-friendly excel report of all the scheduled jobs.

If I don't convert JOB_START_TIME to NUMERIC or DECIMAL, it truncates the time so it is only accurate up to the hour:
SELECT JOB_NAME, MAIL_SERVER, TASK_SERVER, ((((JOB_START_TIME/1000))/60)/60) AS [START_TIME]

So I converted it and what I got was the hour plus a decimal portion representing the fraction of the hour but I don't know how to render that all accurately in hh:mm datetime format:
SELECT JOB_NAME, MAIL_SERVER, TASK_SERVER, ((((CONVERT(NUMERIC,JOB_START_TIME)/1000))/60)/60) AS [START_TIME]

Experts, can you help me get this into a single SQL statement without temp tables, functions, or variables?  I've had difficulty running T-SQL containing the aforementioned from Excel 2003.  Other reporting systems are also not an option.

Thanks!
0
Comment
Question by:BrotherForbes
10 Comments
 
LVL 21

Expert Comment

by:mastoo
ID: 33702377
Hours and minutes?

Select Left( Convert(varchar(10), DateAdd(ms, your number here, Convert(DateTime, '1/1/2000')), 8), 5)
0
 
LVL 7

Expert Comment

by:tlovie
ID: 33702392
Try this:  
-- add the milliseconds to an arbitrary date
-- convert this format to HH:MM:SS


SELECT JOB_NAME, MAIL_SERVER, TASK_SERVER, convert(char(8), dateadd(ms, JOB_START_TIME, '2001-01-01'), 8) AS [START_TIME]
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 33702669
Or this:

SELECT RIGHT('0' + CAST(JOB_START_TIME / 1000 / 60 / 60 AS varchar(2)), 2) + ':' + RIGHT('0' + CAST(JOB_START_TIME / 1000 / 60 % 60 AS varchar(2)), 2)
0
 

Author Comment

by:BrotherForbes
ID: 33702900
Mastoo and ScottPletcher, both your methods are working perfectly in SQL and Excel!  Only one thing I forgot to mention, it is putting the time in 24hr format.  Can we get it in 12 hour format with AM and PM?  OR is that a separate question?
0
 

Author Comment

by:BrotherForbes
ID: 33702932
tlovie: your method is working also, just don't need seconds and need to get it in 12 hour time format.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 7

Accepted Solution

by:
tlovie earned 500 total points
ID: 33703089
Try this:

SELECT JOB_NAME, MAIL_SERVER, TASK_SERVER, substring(convert(varchar(20), dateadd(ms, JOB_START_TIME, '2001-01-01'), 100), 12, 8) AS [START_TIME]
0
 

Author Comment

by:BrotherForbes
ID: 33703169
Thanks tlovie...that is just about perfect... Is it possible to add a space between the mm and the AM/PM indicator?
0
 

Author Closing Comment

by:BrotherForbes
ID: 33703187
This resolves my question
0
 

Author Comment

by:BrotherForbes
ID: 33703219
tlovie, if you want to comment on the question about spacing please do, but I've already given you credit for the answer since it solves my original issue completely, cosmetics aside.  Thanks to all Experts!!
0
 

Author Comment

by:BrotherForbes
ID: 33703322
For future users who might have a similar question here is an example of the data in the table



experts-26481020.xls
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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

910 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

25 Experts available now in Live!

Get 1:1 Help Now