Formatting an SQL numeric query result with an arbitrary number of decimal places

后端 未结 6 398
孤街浪徒
孤街浪徒 2021-01-15 04:51

I have a database table with these two columns:

  • Amount: numeric (18,0)
  • DecimalPlaces: numeric (18,0)

This table can store amounts in va

6条回答
  •  深忆病人
    2021-01-15 05:38

    This is gross but worked for the current inputs on SQL server.

    select 
        substring(
         CAST(
          CAST(
            (amount *  power(-0.100000000000000000,decimalPlaces*1.000000000000000000)) as numeric(36,18)
          )as varchar(30)
         )
        ,1,len(cast(amount as varchar(20))) + (CASE WHEN decimalPlaces = 0 THEN 0 ELSE 1 END )) 
    
    from
     myTable
    

提交回复
热议问题