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

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

    Martlark's answer for Oracle led me to this solution for SQL Server:

    select
      left(cast(Amount as varchar), len(cast(Amount as varchar)) - DecimalPlaces) +
      left('.', DecimalPlaces) +
      right(cast(OriginalCurrencyAmount as varchar), DecimalPlaces
    ) as FormattedAmount
    from MyTable
    

提交回复
热议问题