February 27, 2007 at 9:52 am
I'm going to be doing calculations (addition and division) on data (sales figures in dollars) and am wondering what is the best datatype to use for my tables. The sales figures will have decimial place.
Thank you.
February 27, 2007 at 10:46 am
I'm going to use FLOAT. I've found out that doing division on MONEY gives incorrect results.
February 27, 2007 at 10:50 am
Read up on float before you decide. Float is an "approximate" data type and not always the best choice for the manipulation of real money. I'd use numeric and specify the expected scale and precision.
February 27, 2007 at 11:04 am
I had a table with values for goals (stored with datatype [money]) and was dividing the sum of the values in other tables (stored with datatype [money]) by the value of the goal.
Using [money] for the goal table value datatype, the results would be incorrect. When I changed it to [float] the calculations work correctly.
February 28, 2007 at 12:59 am
Using float for financial values is a bad idea. It's an inaccurate data-type and you may well get rounding errors.
Use either numeric or decimal and set the scale high enough that you get the required accuracy. If you need your calculations accurate to 4 decimal places, use a numeric with a scale of 5 or 6
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 28, 2007 at 8:39 pm
I sure would like to know why you think MONEY gives incorrect results... 4 decimal places isn't enough? Maybe not for interest calcs and monthly payment calcs...
But David and Gail are absolutely correct about FLOAT (and REAL)... if you don't think so, try this simple experiment... and I haven't even touched on divide, yet...
DECLARE @Penny FLOAT
SET @Penny = .01
DECLARE @Money FLOAT
SET @Money = 0
DECLARE @Counter INT
SET @Counter = 1
WHILE @Counter <= 1000
BEGIN
SET @Money = @Money + @Penny
SELECT @Money
SET @Counter = @Counter + 1
END
If you want the accuracy of a scientific or business calculator, use DECIMAL with 15 decimal places and round to only 13 decimal places just like they do.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 28, 2007 at 8:44 pm
...forgot the divide example... you want THIS kind of crappy accuracy? Tell me which company you're writing this for so I can make sure I don't have any stock in it, please
DECLARE @Float1 FLOAT
DECLARE @Float2 FLOAT
SET @Float1 = 10
SET @Float2 = 1
SELECT @Float2/@Float1
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply