Share via

what data type i should use for roversion col and can i use less than greater than with it.

rajesh yadav 291 Reputation points
2026-04-02T07:54:36.2033333+00:00

can i use Rowversioncol ( in my database it's synonyme timestamp is used) to run the following query to find all inseted after this @LastKnownRowVersion;

SELECT * FROM YourTable

WHERE RowVersionCol > @LastKnownRowVersion;

data type i should use for @LastKnownRowVersion; why i am asking is because i read this from

"A nonnullable rowversion column is semantically equivalent to a binary(8) column. A nullable rowversion column is semantically equivalent to a varbinary(8) column."

q2) what should i do if i have non null roversioncol and if i have null roversion col.

SQL Server | SQL Server Transact-SQL

1 answer

Sort by: Most helpful
  1. Marcin Policht 85,255 Reputation points MVP Volunteer Moderator
    2026-04-02T11:16:04.1633333+00:00

    Yep - you should be able to use a rowversion (or timestamp) column in SQL Server to track changes and run a query like

    SELECT * FROM YourTable
    WHERE RowVersionCol > @LastKnownRowVersion;
    

    The data type to use for @LastKnownRowVersion depends on whether the column is nullable or not. Since rowversion is an 8-byte binary value, for a non-nullable rowversion column, you can declare @LastKnownRowVersion as binary(8).

    DECLARE @LastKnownRowVersion binary(8)
    

    If the column were nullable (which is unusual, but possible), you would declare it as varbinary(8):

    DECLARE @LastKnownRowVersion varbinary(8)
    

    For a non-nullable rowversion column, you can safely use binary(8) for your parameter and compare it directly, because rowversion values always exist and are sequentially increasing within the database. If the column is nullable, you would have to handle the possibility of NULL values in your comparisons, but otherwise the same query works.

    The key point is that rowversion is treated as a binary(8) value in SQL Server, so any variable or parameter used to compare against it must be compatible with binary(8) or varbinary(8) depending on nullability.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    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.