Format a Number as Currency in MariaDB

Here’s an example of formatting a number as currency in MariaDB.

SELECT CONCAT('$', FORMAT(8790.2398, 2));

Result:

$8,790.24

Here, we used MariaDB’s CONCAT() function to concatenate the currency symbol and the number. We also used the FORMAT() function to format the number in the desired format.

Specifying a Locale

MariaDB’s FORMAT() function accepts an optional third argument for the locale. This allows you to format the number using the specified locale.

Example:

SELECT CONCAT('€', FORMAT(8790.2398, 2, 'de_DE'));

Result:

€8.790,24

In this case, I specified that the number should be formatted using German numbering conventions, which uses a period for the group separator, and a comma for the decimal point.

I also used the Euro symbol () instead of the dollar sign for the currency symbol.

Formatting in the Application vs Database

It’s usually better that currency and other formatting is done at the application level, rather than at the database level. Application programming environments typically have better formatting capabilities than DBMSs, and it’s usually better to have unformatted data returned by the DB, so that the application can start with a clean slate and format it as needed.

Having said that, sometimes the situation calls for formatting to be done at the database level, in which case, the above techniques can come in handy.