views:

24

answers:

2

The modulus function in Microsoft SQL Server only works on certain data types.

According to the MSDN Article [1] on the modulus operator, you normally would use modulus like this...

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.

However that doesn't work when the dividend is a float data type. The answer we came up with is listed below for future reference.

[1] http://msdn.microsoft.com/en-us/library/ms190279.aspx

A: 

This is the answer I came up with. It only applies if the dividend is a float, not the divisor as well.

( cast(dividend as integer) % divisor ) + ( dividend - cast(dividend as integer))
Great Turtle
+1  A: 

Cast to decimal/numeric, modulo, and cast back?

CAST(CAST(TheInaccurateFloatValue AS decimal(38,19)) % ModuloValue AS float) 
gbn
Another good solution.
Great Turtle