I am creating a SQL table to hold transactions:
create table dbo.TRANSACTIONS
(
Id int identity not null,
Amount money not null
);
For currency (I am using euros) should I use money, decimal, or numeric?
I have seen the three being applied to currency columns so I am not sure anymore.
Money would be the obvious choice ... But I have seen decimal and numeric to.
By the way, I am using SQL Server 2012.
Thank You
The best datatype to use for currency in C# is decimal. The decimal type is a 128-bit data type suitable for financial and monetary calculations. The decimal type can represent values ranging from 1.0 * 10^-28 to approximately 7.9 * 10^28 with 28-29 significant digits.
Money Data Type. The money data type is an abstract data type. Money values are stored significant to two decimal places. These values are rounded to their amounts in dollars and cents or other currency units on input and output, and arithmetic operations on the money data type retain two-decimal-place precision.
If you need the highest precision, a DECIMAL can use up to 17 bytes for each value. Generally though, I like using DECIMAL(19,4) for currency, which needs 9 bytes and can store numbers 19 digits wide, where the last four digits are after the decimal place.
In SQL Server, you can use the T-SQL FORMAT() function to format a number as a currency. The FORMAT() function allows you to format numbers, dates, currencies, etc. It accepts three arguments; the number, the format, and an optional “culture” argument.
First of all, Decimal and Numeric have the same functionality (MSDN info about it)
To answer the new question money VS decimal, there is already a Stackoverflow question about it: Should you choose the MONEY or DECIMAL(x,y) datatypes in SQL Server? - the short answer was:
Never ever should you use money it is not precise and it is pure garbage, always use decimal/numeric
by SQLMenace
Decimal and Numeric are for almost all purposes, equivalent
Money is not a standard Sql type, and there may be other reasons to avoid it as well.
So choose an appropriate scale and precision and use DECIMAL
.
Re : DECIMAL(19,4) vs (20,4)
The precision vs storage table here sums things up.
19,4
will store a value of 999 999 999 999 999.9999
, at a cost of 9 bytes. A precision of 20 would require 13
bytes, which IMO would be a waste unless you needed the extra precision (in which case you can go to a precision of 28 with the same storage).
Also, for the same 9 bytes, you could also use e.g. 19,2
, which will store
99 999 999 999 999 999.99
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With