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

后端 未结 6 425
孤街浪徒
孤街浪徒 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:24

    The best I've been able to come up with so far is:

    select Amount/power(10, DecimalPlaces) from MyTable
    

    But it doesn't do exactly what I want:

    • Oracle: the trailing zeroes are stripped, so US$15.00 looks like "15", not "15.00"
    • SQL Server: a whole lot of extra trailing zeroes are added, so $23.99 looks like "23.99000000000" instead of "23.99"

提交回复
热议问题