Share via

Reversing time values into integer ones

Northface 161 Reputation points
2022-02-09T13:11:55.817+00:00

Hi there,

Using FORMAT and DATEADD in conjunction we can get the time value as expected from a integer format.

DECLARE @iTime INTEGER = 49920
PRINT FORMAT(DATEADD(SECOND,@iTime,'00:00:00'), 'HH:mm','es-es') + ':00.000'

What about the reverse way?

ex:

DECLARE @dTime AS TIME = '19:30:00.456'

How can we get the integer value equivalent of that?

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Olaf Helper 47,621 Reputation points
2022-02-09T13:20:22.617+00:00

For integer => time you use DATEADD, so logically for time => integer you use DATEDIFF

DECLARE @dTime AS TIME = '13:52:00.000'
SELECT DATEDIFF(second, '00:00:00', @dTime)

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Northface 161 Reputation points
    2022-02-09T13:59:13.047+00:00

    Thanks a lot Olaf

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.