Skip to content

Instantly share code, notes, and snippets.

@JerryNixon
Last active September 14, 2020 17:28
Show Gist options
  • Select an option

  • Save JerryNixon/ab533c31ab32a86ae28da5125a51c37b to your computer and use it in GitHub Desktop.

Select an option

Save JerryNixon/ab533c31ab32a86ae28da5125a51c37b to your computer and use it in GitHub Desktop.
Convert milliseconds to Hours/Minutes/Seconds printable format in TSQL.
declare @ms bigint = 123456789;
declare @start datetime = '1/1/2000';
declare @date datetime = dateadd(millisecond, @ms, @start);
declare @day_diff int = datediff(day, @start, @date);
declare @time time = cast(@date as time);
declare @value varchar(max) = concat(
@day_diff, ' Day(s) ',
format(@time, 'hh'), ' Hour(s) ',
format(@time, 'mm'), ' Minute(s) ',
format(@time, 'ss'), ' Seconds(s) ',
format(@time, 'fff'), ' Milliseconds(s) ');
print @value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment