help

  • On dividing two integer columns in a sql table how to get the result as a number with 2 decimal places.

    example

    select Elevation/Height As TValue from Camps

    (where Elevation and Height are integer columns and I want the resultant TValue as a number with 2 decimal places)

    Thanks a lot.

  • select Elevation/Height*1.00 As TValue from Camps should work, but why as this will always be .00?

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Thanks but no I do not want it to be always .00?

    example if Elevation/Height (40/3)=13.33 the result should be 13.33 and not 13.00

  • Would select cast(Elevation as decimal(8,2))/Height As TValue from Camps help?

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • No luck it still shows as 13.3333333333333 and not 13.33

     

  • If you're using the result as a number, cast it to decimal.  If you're just displaying the result, you can use STR().

    SELECT CAST(Elevation*1./Height AS dec(9,2)), STR(Elevation*1./Height,10,2)

    Multiplying by 1. is the lazy programmer's way to force an implicit conversion to decimal(n,0), which works in this case as you only want a scale of 2 and the minimum scale is 6 for the result of a division where any operand is decimal. 



    --Jonathan

  • So, you seem to need this for presentational purposes?

    Well, use Jonathan's suggestion or, even better IMHO, do this in your application layer.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Ok got it

    Thanks all

Viewing 8 posts - 1 through 7 (of 7 total)

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