Display USD currency records with the correct format in MySQL

MySQLMySQLi Database

Use FORMAT() in MySQL to display USD currency records in the correct form. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Amount DECIMAL(15,4)
   -> );
Query OK, 0 rows affected (0.75 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(90948484);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(1000000000);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(1535353536);
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values(773646463);
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output−

+-----------------+
| Amount          |
+-----------------+
| 90948484.0000   |
| 1000000000.0000 |
| 1535353536.0000 |
| 773646463.0000  |
+-----------------+
4 rows in set (0.00 sec)

Here is the query to display currency records correctly in MySQL−

mysql> select concat('USD ',format(Amount,2)) as Currency from DemoTable;

This will produce the following output. Here, we are displaying USD currency records in the correct format−

+----------------------+
| Currency             |
+----------------------+
| USD 90,948,484.00    |
| USD 1,000,000,000.00 |
| USD 1,535,353,536.00 |
| USD 773,646,463.00   |
+----------------------+
4 rows in set (0.23 sec)
raja
Published on 12-Dec-2019 06:28:04
Advertisements