There are several ways to format a number as a percentage in SQL, depending on the DBMS being used.
Here are examples of adding a percent sign to a number in some of the most popular DBMSs.
MySQL
In MySQL, we can use the CONCAT()
function to concatenate the number and the percent sign:
SELECT CONCAT(3.75, '%');
Result:
3.75%
See Format a Number as a Percentage in MySQL for more.
SQL Server
SQL Server’s FORMAT()
function provides us with the P
format specifier, which is used to display a percent sign in the formatted number:
SELECT FORMAT(.27, 'P') Result;
Result:
+----------+ | Result | |----------| | 27.00% | +----------+
See 4 Ways to Convert a Number to a Percentage in SQL Server for more.
Oracle
Oracle Database has a TO_CHAR(number)
function that we can use to format a number with. We can then use CONCAT()
to concatenate the number and the percent sign:
SELECT CONCAT(TO_CHAR(18, 'fm00D00'), '%')
FROM DUAL;
Result:
18.00%
See Format a Number as a Percentage in Oracle for more.
PostgreSQL
In PostgreSQL, we can use the TO_CHAR()
function to format a number as a percentage.
Here are various examples to demonstrate some of the things we can do with this function:
SELECT
TO_CHAR(7, 'fm00%') AS "1",
TO_CHAR(7, 'fm99%') AS "2",
TO_CHAR(7.4567, 'fm0D00%') AS "3",
TO_CHAR(7, 'fm0D00%') AS "4";
Result:
+-----+----+-------+-------+ | 1 | 2 | 3 | 4 | +-----+----+-------+-------+ | 07% | 7% | 7.46% | 7.00% | +-----+----+-------+-------+
See 3 Ways to Format a Number as a Percentage in PostgreSQL for more.
MariaDB
MariaDB has a CONCAT()
function to do the job:
SELECT CONCAT(7.45, '%');
Result:
7.45%
See Add a Percent Sign to a Number in MariaDB for more.
SQLite
With SQLite, we can use the PRINTF()
function:
SELECT PRINTF('%2d%%', 17);
Result:
17%
We can alternatively concatenate the percent sign and the number if required. See 2 Ways to Add a Percent Sign to a Number in SQLite for an example.
Update: SQLite 3.38.0 (released 22 Feb 2022) renamed the PRINTF()
function to FORMAT()
. The original PRINTF()
name is retained as an alias for backwards compatibility.
So the above example can be changed to this:
SELECT FORMAT('%2d%%', 17);
Result:
17%