displaying decimal values in QA

  • I am trying to get the result of this very straight forward division command

    select 1000 / 34 --format as 9,999

    in Query Analyzer

    but I get

    -----------

    29

    Is there a possibility of of getting the result as 29,411

  • SQL Server is doing integer division, as it has correctly guessed that 1000 and 34 are integers.

    If you force one of the values to be decimal, SQL Server will do decimal division, and you can then correctly format the result:-

    select convert(decimal(10,3),convert(decimal,1000)/34)

  • Thanks

  • You can also do this to force decimal division:

    select 1000.0 / 34.0

    or something like this is you want to round:

    select cast(1000.0 / 34.0 as decimal(10,3))

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply