Modulo operator, how to do Binary % bigint?

时间:2015-08-06 13:54:52

标签: sql-server binary-data modulo bigint

MSDN says that valid types for Modulo are

data types in the integer and monetary data type categories, or the numeric data type

BINARY is not in a list, but still I can do

DECLARE 
  @a bigint = 1, 
  @b bigint = 2, 
  @bin binary(16)

SET @bin = cast(@a AS binary(8)) + cast(@b AS binary(8))

PRINT @bin % 2147483647

BIGINT is in the list, but this doesn't work:

PRINT @bin % 9223372036854775807

My question, is it possible to do BINARY % BIGINT?

1 个答案:

答案 0 :(得分:0)

在第二个示例中,SQL Server隐式将9223372036854775807视为numeric(十进制),因此%运算符不支持。

您只需先将其转换为bigint

DECLARE 
  @a bigint = 1, 
  @b bigint = 2, 
  @bin binary(16)

SET @bin = cast(@a AS binary(8)) + cast(@b AS binary(8))

PRINT @bin % 2147483647
PRINT @bin % CONVERT(bigint, 9223372036854775807)

[我认为]这是因为numericdecimal)的类型优先级高于bigint