home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.databases.ms-sqlserver      Notorious Rube Goldberg contraption      19,505 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 18,781 of 19,505   
   Erland Sommarskog to Gene Wirchenko   
   Re: SSE2008 money Type   
   11 Oct 12 11:16:30   
   
   XPost: microsoft.public.sqlserver.programming   
   From: esquel@sommarskog.se   
      
   Gene Wirchenko (genew@ocis.net) writes:   
   > print 12.345;     -- prints 12.345   
   > print convert(money,12.345);     -- prints 12.35   
   > print 12.35-convert(money,12.345);     -- prints 0.0050   
   > print convert(money,12.35)-convert(money,12.345);     -- prints 0.01   
   > print 12.345+convert(money,12.345)-12.345;     -- prints 12.3450   
   >   
   >      The precision is apparently there per the third statement, but   
   > how do I force it?  I have tried variations with the second statement,   
   > but I have been unable to get it to output 12.345.  How is it that the   
   > third statement gives four decimal digits, but the second and fourth   
   > do not?  The fifth statement is just plain weird.   
      
   You are confusing precision with print format and the conversion to varchar.   
      
   Apparently money is converted to varchar without display of trailing zeroes.   
   The decimal data type is not. And when you add a number like 12.35 you are   
   in decimal land, since the data type of 12.35 is decimal(5,2).   
      
   Use the str() function to get trailing zeroes:   
      
   declare @m money = 1.234   
   select str(@m, 6, 4)   
      
      
   --   
   Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se   
      
   Books Online for SQL Server 2005 at   
   http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx   
   Books Online for SQL Server 2000 at   
   http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca