Solved

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

Posted on 2010-09-17
10
2,667 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:Scott Pletcher
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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
 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

776 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