Solved

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

Posted on 2010-09-17
10
2,606 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

747 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

9 Experts available now in Live!

Get 1:1 Help Now