What's the main difference between these two data types and which would best
fit the purpose?
I want to store signed numerical data with up to 6 decimal places.
Thanks in advance for any help.
DOUBLE is a floating point number and is, therefore, not precise. Use
DECIMAL (or NUMERIC). For example, you might use:
DECIMAL (19,6) which has 14 digits before the decimal and 6 digits
afterward and is stored in 9 bytes.
Read about DECIMAL and storage considerations:
http://msdn.microsoft.com/en-us/library/ms187746.aspx
Also data type conversions:
http://msdn.microsoft.com/en-us/library/ms191530.aspx
RLF
"Andy Trezise" <an...@trezise.f2s.com> wrote in message
news:uTnMZZtc...@TK2MSFTNGP06.phx.gbl...
"Russell Fields" <russel...@nomail.com> wrote in message
news:%23NaRost...@TK2MSFTNGP05.phx.gbl...
declare @m money
declare @d decimal(9,2)
set @m = 234.56
set @d = 234.56
select cast((@m/1000)*1000 As money)
select cast((@d/1000)*1000 As decimal(9,2))
Note that the result of the calcuation using money type returns the value
234.50 NOT 234.56.
Tom
"Rick Byham, MSFT" <ric...@microsoft.com> wrote in message
news:eOlkfi3c...@TK2MSFTNGP06.phx.gbl...
set @m = 234.56
set @d = 234.56
select cast((@m/1000) As money) AS UsingMoney
select cast((@d/1000) As decimal(9,2)) AS UsingDecimal
Result:
UsingMoney
0.2345
UsingDecimal
0.23
You just walked away with 0.0045 dollars. Run this transaction a million
times and you'll wind up in jail. That's why generally accepted accounting
principles defined money with 4 decimal points. The money data type doesn't
have math problems, it just follows a specific set of accounting rules.Which
may or may not apply to your specific use.
Disclaimer - I got my finance degree in 1973. Things may have changed a lot
since then.
--
Rick Byham, MSFT
(Implies no warranty or rights)
"Tom Cooper" <tomc...@comcast.net> wrote in message
news:ue%2327y4c...@TK2MSFTNGP04.phx.gbl...
Pointing out that if you put .2345 into a decimal(9,2) column or variable,
you will get .23 is equivalent to pointing out that if you put "HELP" into a
char(2) variable you get "HE". It's true, but, IMO, not helpful. The fix
is not to give up on type CHAR datatype, it's to use CHAR(4) or VARCHAR(4)
or the Unicode (NCHAR or NVARCHAR), as needed for the values you need to
store.
But there is no way to fix the calculation error with the MONEY datatype
unless you first cast the MONEY as a decimal before doing the calculation.
And if you are going to do that, you might as well have used decimal in the
first place.
Tom
"Rick Byham, MSFT" <ric...@microsoft.com> wrote in message
news:OGcyQa6c...@TK2MSFTNGP06.phx.gbl...
"Tom Cooper" <tomc...@comcast.net> wrote in message
news:urYuHe9c...@TK2MSFTNGP06.phx.gbl...