How to convert to two decimal places

  • Hi

    I have this code here

    case

    when chargeType <> 'ADV_SWITCH_FEE'

    then CAST(isNull(sum(chargeAmount)/100.0, 0.0) AS VARCHAR)

    else ''

    end as chargeAmount

    How do I convert my "sum" into decimal places?

  • You will need to convert it to a data type which allows you to specify the amount of decimal places you want, you would want decimal with the correct precision to cope with how ever many decimal places you need.

  • is your chargeAmount is INT then you need to convert it to decimal to get decimal in SUM

  • My charge amount need to cater for both "Then" and "Else", my "else" returns string hence I cast it as varchar. that code so far is working but I need to return two decimal value amount.

  • cast the inner sum as decimal then case the whole output as varchar

  • Use STR function. E.g:

    declare @x money=12345.69851

    SELECT [STR]=STR(@x, 20, 2), [CONVERT_default]=CONVERT(varchar(20), @x), [CONVERT_format_1]=CONVERT(varchar(20), @x, 1), [CONVERT_format_2]=CONVERT(varchar(20), @x, 2)

    _____________________________________________________
    Microsoft Certified Master: SQL Server 2008
    XDetails Addin - for SQL Developers
    blog.sqlxdetails.com - Transaction log myths

Viewing 6 posts - 1 through 5 (of 5 total)

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