Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Decimal places in Money?

Author  Topic 

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2011-11-11 : 15:57:18

I think this is a weird one. I understand money has 4 decimal places but why when I cast it to varchar, it displays as only 2?


declare @x money
set @x = 9.11111

print cast (@x as varchar)
print cast (@x as numeric(10, 5))

malpashaa
Constraint Violating Yak Guru

264 Posts

Posted - 2011-11-11 : 20:32:29
When you convert from MONEY to VARCHAR using CAST; the conversion use style 0 be default, which take two digits to the right of the decimal point. If you want to keep the four digits to the right of the decimal point you should use CONVERT with style 2.


DECLARE @x MONEY = 9.11111;

SELECT CAST(@x as VARCHAR) AS [CAST],
CONVERT(VARCHAR, @x, 0) AS [CONVERT with style 0],
CONVERT(VARCHAR, @x, 2) AS [CONVERT with style 2]



For us, there is only the trying. The rest is not our business. ~T.S. Eliot

Muhammad Al Pasha
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-12 : 05:42:30
quote:
Originally posted by denis_the_thief


I think this is a weird one. I understand money has 4 decimal places but why when I cast it to varchar, it displays as only 2?


declare @x money
set @x = 9.11111

print cast (@x as varchar)
print cast (@x as numeric(10, 5))



AS a sidenote, always specify a length when you cast values to varchar

see effects of not specifying length below

http://visakhm.blogspot.com/2010/02/importance-of-specifying-length-in.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -