SQL 2008 Math is failing in WHERE clause
Using Microsoft SQL Server 2008. I have a table of interest rates. I've manually verified that the data I'm trying to select does exist in the table.
开发者_如何学CThis returns records:
SELECT *
FROM [MyTable]
WHERE [Rate]=3.99/100
This does not return records:
SELECT *
FROM [MyTable]
WHERE [Rate]*100=3.99
Here's the really messed up part: As far as I can tell, this only happens when the Rate field contains a value of .035
, .03625
, or .0399
.
Can anyone tell me why the second instance does not work? This table does have an ID field (PK), and the Rate
field is FLOAT
. Please let me know if you need any more info.
You should use decimal not floats for interest rates. float
is imprecise and you encounter rounding errors as you have discovered!
On my machine
WITH [MyTable]([Rate])
AS (SELECT CAST(.0399 AS FLOAT))
SELECT [Rate] * 100,
CASE
WHEN [Rate] * 100 = 3.99 THEN 'Y' ELSE 'N'
END AS [= 3.99],
CASE
WHEN [Rate] * 100 > 3.989999999999999 THEN 'Y' ELSE 'N'
END AS [> 3.989999999999999],
CASE
WHEN [Rate] * 100 < 3.99 THEN 'Y' ELSE 'N'
END AS [< 3.99]
FROM [MyTable]
Returns
= 3.99 > 3.989999999999999 < 3.99
---------------------- ------ ------------------- ------
3.99 N Y Y
Obligatory Link: What Every Computer Scientist Should Know About Floating-Point Arithmetic
精彩评论