Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Two fields from two tables of same value not equal Expand / Collapse
Author
Message
Posted Friday, October 18, 2013 2:34 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 25, 2013 8:12 AM
Points: 111, Visits: 343
Hi,

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.CustNo
WHERE cast(t1.TotalRevenue as float) <> tmp.CntRev

TotalRevenue 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_AI
Post #1506019
Posted Friday, October 18, 2013 2:48 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 10:15 AM
Points: 2,663, Visits: 5,603
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





Post #1506023
Posted Friday, October 18, 2013 2:48 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, March 25, 2014 7:22 AM
Points: 77, Visits: 150
Float and real are approximate data types. Therefore you should use decimal.

Jonathan Bernardez Bernardez
___________________________________________________________
DBD. MCSA SQL Server 2012
Post #1506024
Posted Friday, October 18, 2013 2:57 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 9:13 AM
Points: 6, Visits: 85
Float 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:

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;

Post #1506031
Posted Friday, October 18, 2013 3:04 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 25, 2013 8:12 AM
Points: 111, Visits: 343
Hi all,

Thanks. CAST to decimal has worked.

Post #1506035
Posted Friday, October 18, 2013 3:23 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 10:15 AM
Points: 2,663, Visits: 5,603
While 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 John





Post #1506044
Posted Saturday, October 19, 2013 7:29 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 4:44 PM
Points: 8,286, Visits: 8,736
Mike John (10/18/2013)
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

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.


Tom
Post #1506397
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse