Hey guys,
Good afternoon!
These last few days I've been a little short on time, but to avoid updating here I'm going to make another quick post demonstrating how to convert milliseconds, seconds or minutes to TIME in SQL Server.
Many times in our routines, especially when we create routine execution logs measuring the time taken for processing, we need to store these records in a TIME field or a string that simulates the behavior of the TIME data type (Ex: 00:01:08.57800)
However, the conversion, despite being simple, is not so trivial and raises many doubts, especially among developers new to SQL Server who end up using functions to perform this task and applying them to large volumes of data, compromising the performance of the query. For this reason, I decided to create this post to help with this question.
-- Converter 5874502 Milisegundos para tempo
SELECT CONVERT(TIME, DATEADD(MILLISECOND, 5874502 + 86400000, 0), 114)
-- Converter 587 Segundos para tempo
SELECT CONVERT(TIME, DATEADD(SECOND, 587 + 86400000, 0), 114)
-- Converter 457 Minutos para tempo
SELECT CONVERT(TIME, DATEADD(MINUTE, 457 + 86400000, 0), 114)
-- Converter 5874502 Milisegundos para string
SELECT CONVERT(VARCHAR(12), DATEADD(MILLISECOND, 5874502 + 86400000, 0), 114)
Query result:

Until next time!
How to convert milliseconds milliseconds, seconds or minutes to TIME in SQL Server, convert seconds minutes milliseconds to time
How to convert milliseconds milliseconds, seconds or minutes to TIME in SQL Server, convert seconds minutes milliseconds to time
Comentários (0)
Carregando comentários…