Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting number as currency amount

I want to create a "sales report" for my SQL pizza restaurant database. When I try to run the SELECT query with sum(order_amount), the column isn't formatted. I'm using Oracle SQL. Here is the code:

column order_amount heading "Order Amount" format '$9,999,999.00';
select order_id, count(order_id) as "Number of Orders", sum(pizza_quantity) as "Pizzas Sold", 
sum(order_amount) as "Total Profit"
from order_details group by order_id;

And here is the output:

SQL> select order_id, count(order_id) as "Number of Orders",sum(pizza_quantity) as "Pizzas Sold",
  2  sum(order_amount) as "Total Profit"
  3  from order_details group by order_id;

  Order ID Number of Orders Pizzas Sold Total Profit
---------- ---------------- ----------- ------------
         1                2           5          105
         6                1           2           20
         2                2           6          165
         4                2          12          130
         5                2           6           80
         8                1           7          140
         3                2           4           80
         7                1           6          120
         9                1           8          120
        10                1           9          270

10 rows selected.

Is there any way to display the Total Profit column formatted as currency?

like image 791
Constantin Buruiana Avatar asked Jan 26 '26 16:01

Constantin Buruiana


2 Answers

You can try using to_char().

...
to_char(sum(order_amount), '$9,999,999.00')
...
like image 66
sticky bit Avatar answered Jan 29 '26 06:01

sticky bit


You might use number format modelling elements as FML9,999.99 or $9G990D00 for the second argument of to_char function as in the following sample :

SQL> alter session set nls_currency='$';
SQL> alter session set NLS_NUMERIC_CHARACTERS = '.,';
SQL> with tab(order_ID,order_amount) as
  (
    select 1,255.51 from dual union all
    select 1,750.33 from dual union all    
    select 6,420.39 from dual union all
    select 2,790.00 from dual union all
    select 2,375.05 from dual    
  )
  select to_char(sum(order_amount),'FML9,999.99') as "Total Profit" 
    from tab
   group by order_ID  
  union all
  select to_char(sum(order_amount),'$9G990D00') as "Total Profit" 
    from tab
   group by order_ID

Total Profit
$1,005.84
$420.39
$1,065.05
 $1,005.84
   $420.39
   $1,065.05

Rextester Demo

like image 35
Barbaros Özhan Avatar answered Jan 29 '26 06:01

Barbaros Özhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!