Cast rowversion to bigint

前端 未结 2 1998
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 06:33

In my C# program I don\'t want to work with byte array, therefore I cast rowversion data type to bigint:

SELECT CAST([version] AS BIGINT) FROM [dbo].[mytable         


        
2条回答
  •  遥遥无期
    2021-01-18 07:13

    rowversion and bigint both take 8 bytes so casting seems possible. However, the difference is that bigint is a signed integer, while rowversion is not.

    This is a max value of rowversion that will cast properly to max positive bigint number (9223372036854775807):

    select cast(0x7FFFFFFFFFFFFFFF as bigint)

    But starting from here, you'll be getting negative numbers:

    select cast(0x8000000000000000 as bigint)

    I didn't check if the latter cast throws an error in C#.

    You problably won't reach more than 9223372036854775807 rows in your table, but still it's something you should know about, and I personally wouldn't recommend doing this unless you are certain that this problem will never occur in your solution.

提交回复
热议问题