SQL Server MONEY Data Type

MONEY is a data type in SQL Server used to represent monetary values. It supports a range of values from -922,337,203,685,477.5808 to 922,337,203,685,477.5807 with a precision of four decimal places.

Syntax

The syntax for the MONEY data type is as follows:

MONEY

Use cases

The MONEY data type is suitable for storing monetary values and is often used in financial applications such as bank accounts, stock prices, and salaries. When performing currency calculations, the MONEY data type should be used because it provides higher precision and better performance.

Examples

The following is an example using the MONEY data type to store the balance of a bank account:

CREATE TABLE BankAccount (
    ID INT PRIMARY KEY,
    Balance MONEY NOT NULL
);

INSERT INTO BankAccount (ID, Balance)
VALUES (1, 12345.67),
       (2, 98765.43),
       (3, 45678.90);

SELECT * FROM BankAccount;

In the example above, we create a table named BankAccount with two fields, ID and Balance. The Balance field uses the MONEY data type with a precision of four decimal places. We insert three records, each containing an ID and a balance.

The following is an example using the MONEY data type to calculate the total value of two stocks:

DECLARE @Price1 MONEY = 123.45;
DECLARE @Price2 MONEY = 67.89;
DECLARE @TotalPrice MONEY;

SET @TotalPrice = @Price1 + @Price2;

SELECT @TotalPrice;

In the example above, we declare two variables of the MONEY data type, @Price1 and @Price2, with values of 123.45 and 67.89 respectively. We then declare another variable of the MONEY data type, @TotalPrice, and add @Price1 and @Price2 to get the total value. Finally, we output the value of the @TotalPrice variable.

Conclusion

The MONEY data type is a data type in SQL Server used to represent monetary values. It supports a range of values from -922,337,203,685,477.5808 to 922,337,203,685,477.5807 with a precision of four decimal places. The MONEY data type is suitable for currency calculations in financial applications. When performing currency calculations, the MONEY data type should be used because it provides higher precision and better performance.