When I use a float variable to do some math, SQL is not seeing the result as equal to the same math if I were to hardcode the number instead. Compare the results from the two queries below to see what I mean.DECLARE @recordnumber floatSET @recordnumber = 4060.012SELECT@recordnumber AS SelectedRecord,(@recordnumber + .001) AS MathNextRecord,4060.013 AS HardCodeNextRecord,CASEWHEN (@recordnumber + .001) = 4060.013THEN 'Yes'ELSE 'No'END AS Equal
This returns the following:SelectedRecord: 4060.012MathNextRecord: 4060.013HardCodeNextRecord: 4060.013Equal: No (???)If I adjust the code to use the same math, but instead hardcode '4060.012' instead of using the variable, it works as expected. SELECT4060.012 AS SelectedRecord,(4060.012 + .001) AS MathNextRecord,4060.013 AS HardCodeNextRecord,CASEWHEN (4060.012 + .001) = 4060.013THEN 'Yes'ELSE 'No'END AS Equal
This returns the following:SelectedRecord: 4060.012MathNextRecord: 4060.013HardCodeNextRecord: 4060.013Equal: YesWhat in the world is going on here? I need to be able to use variables to do calculations, but it's not very helpful when SQL doesn't see the resulting number as equal to what you would expect.Any explanation on why this might be happening and/or how to fix this would be very helpful!UPDATE:I found out that a 4-byte float variable does work, but the default 8-byte float variable doesn't work.DECLARE @recordnumber float(24)
WorksDECLARE @recordnumber float
Doesn't work"Float(n): Floating precision number data from -1.79E + 308 to 1.79E + 308.The n parameter indicates whether the field should hold 4 or 8 bytes. float(24) holds a 4-byte field and float(53) holds an 8-byte field. Default value of n is 53."[url]http://www.w3schools.com/sql/sql_datatypes.asp[/url]I still would like some insight to this strange behavior. Besides guess-and-check, is there any way to know when to use a special parameter for my float variable(s)?