Learn how to get the time from a rowversion in T-SQL with this comprehensive guide. Follow our step-by-step instructions and use our code examples to easily extract the time from a rowversion in T-SQL.

As a SQL developer, you may find yourself needing to extract the time from a rowversion (also known as a timestamp) in T-SQL. Rowversion is a binary data type that is automatically updated every time a row is inserted or updated in a table. In this article, we'll show you how to get the time from a rowversion in T-SQL with step-by-step instructions and code examples.
Contents:
- Understanding Rowversion in T-SQL
- Extracting Time from Rowversion in T-SQL
- Example Code for Extracting Time from Rowversion in T-SQL
- Summary
Understanding Rowversion in T-SQL
Rowversion is a special-purpose data type in T-SQL that is used to track changes to a table. It is also known as a timestamp data type, although it has nothing to do with the actual date and time. Instead, a rowversion is a binary value that is updated automatically every time a row is inserted or updated in a table. This makes rowversion useful for tracking changes to a table and for detecting conflicts when multiple users are updating the same data.
Extracting Time from Rowversion in T-SQL
To extract the time from a rowversion in T-SQL, you can use the built-in function CONVERT. The CONVERT function can be used to convert the binary value of a rowversion to a datetime data type. Once you have converted the rowversion to a datetime value, you can then extract the time component using the built-in function DATEPART.
Example Code for Extracting Time from Rowversion in T-SQL
Here is an example code that demonstrates how to extract the time from a rowversion in T-SQL:
DECLARE @rv rowversion = 0x00000000000007D4 SELECT DATEADD(ms, CONVERT(BIGINT, SUBSTRING(@rv, 1, 8), 1) / 10000 % 86400000, CONVERT(DATETIME2, '19700101')), DATEPART(hour, DATEADD(ms, CONVERT(BIGINT, SUBSTRING(@rv, 1, 8), 1) / 10000 % 86400000, CONVERT(DATETIME2, '19700101'))), DATEPART(minute, DATEADD(ms, CONVERT(BIGINT, SUBSTRING(@rv, 1, 8), 1) / 10000 % 86400000, CONVERT(DATETIME2, '19700101'))), DATEPART(second, DATEADD(ms, CONVERT(BIGINT, SUBSTRING(@rv, 1, 8), 1) / 10000 % 86400000, CONVERT(DATETIME2, '19700101')))
In this example, we first declare a rowversion variable and assign it a value of 0x00000000000007D4. We then use the CONVERT function to convert the rowversion to a datetime value and extract the hour, minute, and second components using the DATEPART function.
Summary
Getting the time from a rowversion in T-SQL is a useful skill for SQL developers. By using the CONVERT and DATEPART functions, you can easily extract the time component from a rowversion value. With the code examples provided in this article, you should be able to apply this technique in your own T-SQL scripts and applications.