SQLServerCentral.com / SQL Server 2008 - General / SQL Server 2008 / Two fields from two tables of same value not equal / Latest PostsInstantForum.NET v2.9.0SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comThu, 18 Dec 2014 10:44:30 GMT20RE: Two fields from two tables of same value not equalhttp://www.sqlservercentral.com/Forums/Topic1506019-391-1.aspx[quote][b]Mike John (10/18/2013)[/b][hr]Float is not an exact data type. The internal storage format can mean rounding is being done that may not be visible to you. It would be interesting to get the exact internal value for both of these, just retrieve the varbinary conversion of them both and compare that. I expect you will see they are very slightly different.As a general rule using float for monetary values is not a good idea, I would either use money (if the maths rules it uses are OK for you) or more likely numeric or decimal with an appropriate scale and precision for your needs.Mike John[/quote]Yes, the problem is most likely happening because FLOAT is not an exact numeric. And the solution (assuming exact numeric arithmetic is accurate enough - "exact" in the name refers to accuracy of representation of base 10 fractions, not to the accuracy of long chains of calculation) and has adequate range) would be to switch to using an exact numeric representation. If that isn't possible, just cast the numbers to an appropriate exact numeric type before comparing them and see if that works.But the problem here may not be rounding; rounding in FLOAT generally introduces much smaller errors than rounding in exact numeric types (which usually throw away quite a lot of accuracy whenever they put the result of a computation into a field of predefined and precision). This doesn't make any difference to what the solution is, until we have a version of SQL that supports modern floating point standards. FLOAT does introduce a particular rounding error which decimal doesn't because SQL hasn't kept up with the recognised international standard on floating point, the representation error introduced by using a binary base for numbers which were expressed originally in base 10 - for example a quantity like 0.2 has no finite binary representation,so it's going to be rounded to something that does have a finite (53 bit, in the case of float) binary representation and that could be the source of the problem; but it's just as likely that the floating point arithmetic that calculated the numbers, because it doesn't introduce fairly large rounding errors every time it tores a value, has produced more accurate answers than exact numerics would and that the error is that the FLOAT rounding error is not big enough. Since the two values were calculated down different paths, the total rounding error will probbly be different in the two cases, which means the two answers will be different unless you round them to something a bit less accurate.Sat, 19 Oct 2013 07:29:35 GMTTomThomsonRE: Two fields from two tables of same value not equalhttp://www.sqlservercentral.com/Forums/Topic1506019-391-1.aspxWhile cast to decimal may have worked in this instance, I think looking at the table definition and changing the underlying datatype there would be a better option - assuming you have the "power" to do so.I appreciate it may be too hard/risky/politically impossible to do so.Mike JohnFri, 18 Oct 2013 03:23:10 GMTMike JohnRE: Two fields from two tables of same value not equalhttp://www.sqlservercentral.com/Forums/Topic1506019-391-1.aspxHi all, Thanks. CAST to decimal has worked.Fri, 18 Oct 2013 03:04:13 GMTa2zwdRE: Two fields from two tables of same value not equalhttp://www.sqlservercentral.com/Forums/Topic1506019-391-1.aspxFloat can have different precision: float(n) where 1 <= n <= 53. According to BOL, 1 <= n <= 23 is treated the same, and 24 <= n <= 53 the same. Is this maybe the cause, that the two columns are both float, but of different in precision?Example: The second select clause will not return the row, the third will return the row:[code]create table #floats(f1 float(12) not null, f2 float(53) not null);insert #floats values(PI(), PI());select * from #floats;select * from #floats where f1 = f2;select * from #floats where f1 = Cast(f2 as float(12))drop table #floats;[/code]Fri, 18 Oct 2013 02:57:34 GMTL-P LarssonRE: Two fields from two tables of same value not equalhttp://www.sqlservercentral.com/Forums/Topic1506019-391-1.aspxFloat and real are approximate data types. Therefore you should use decimal.Fri, 18 Oct 2013 02:48:57 GMTjonysuiseRE: Two fields from two tables of same value not equalhttp://www.sqlservercentral.com/Forums/Topic1506019-391-1.aspxFloat is not an exact data type. The internal storage format can mean rounding is being done that may not be visible to you. It would be interesting to get the exact internal value for both of these, just retrieve the varbinary conversion of them both and compare that. I expect you will see they are very slightly different.As a general rule using float for monetary values is not a good idea, I would either use money (if the maths rules it uses are OK for you) or more likely numeric or decimal with an appropriate scale and precision for your needs.Mike JohnFri, 18 Oct 2013 02:48:18 GMTMike JohnTwo fields from two tables of same value not equalhttp://www.sqlservercentral.com/Forums/Topic1506019-391-1.aspxHi, I have 2 tables which has 2 float fields each.I am joining these two tables and comparing two float fields whether they are equal or not.Surprisingly, 2 fields with the same value returning not equal.SELECT *FROM Table1 t1 INNER JOIN @tab tmp ON t1.CustomerNo = tmp.CustNoWHERE cast(t1.TotalRevenue as float) <> tmp.CntRevTotalRevenue and CntRev has the same value 33679.8 but above query returns rows(means values doesn't match)How come?Database collation is SQL_Latin1_General_CP1_CI_AIFri, 18 Oct 2013 02:34:06 GMTa2zwd