Microsoft SQL Server中的模数函数仅适用于某些数据类型。
根据关于模数运算符的MSDN文章[1],您通常会使用这样的模数...
dividend % divisor
dividend
Is the numeric expression to divide. dividend must be a valid
expression of any one of the data types in the integer and
monetary data type categories, or the numeric data type.
divisor
Is the numeric expression by which to divide the dividend.
divisor must be any valid expression of any one of the data
types in the integer and monetary data type categories, or
the numeric data type.
但是,当被除数是浮点数据类型时,这不起作用。我们提出的答案列在下面以供将来参考。
答案 0 :(得分:5)
转换为十进制/数字,模数和强制转换?
CAST(CAST(TheInaccurateFloatValue AS decimal(38,19)) % ModuloValue AS float)
答案 1 :(得分:2)
declare @A float = 2.5
declare @B float = 1.1
-- Expected: A % B = 2.5 % 1.1 = 0.3
select @A - floor(@A / @B) * @B
答案 2 :(得分:1)
这是我想出的答案。它仅适用于股息是浮点数,而不是除数。
( cast(dividend as integer) % divisor ) + ( dividend - cast(dividend as integer))