Should you choose the MONEY or DECIMAL(x,y) datatypes in SQL Server?

前端 未结 12 2580
时光取名叫无心
时光取名叫无心 2020-11-22 03:45

I\'m curious as to whether or not there is a real difference between the money datatype and something like decimal(19,4) (which is what money uses

12条回答
  •  梦谈多话
    2020-11-22 04:09

    SQLMenace said money is inexact. But you don't multiply/divide money by money! How much is 3 dollars times 50 cents? 150 dollarcents? You multiply/divide money by scalars, which should be decimal.

    DECLARE
    @mon1 MONEY,
    @mon4 MONEY,
    @num1 DECIMAL(19,4),
    @num2 DECIMAL(19,4),
    @num3 DECIMAL(19,4),
    @num4 DECIMAL(19,4)
    
    SELECT
    @mon1 = 100,
    @num1 = 100, @num2 = 339, @num3 = 10000
    
    SET @mon4 = @mon1/@num2*@num3
    SET @num4 = @num1/@num2*@num3
    
    SELECT @mon4 AS moneyresult,
    @num4 AS numericresult
    

    Results in the correct result:

    moneyresult           numericresult
    --------------------- ---------------------------------------
    2949.8525             2949.8525

    money is good as long as you don't need more than 4 decimal digits, and you make sure your scalars - which do not represent money - are decimals.

提交回复
热议问题