Add a percentage (%) sign at the end to each value while using MySQL SELECT statement

MySQLMySQLi Database

To add percentage sign at the end, use CONCAT() function. Let us first create a table −

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(100),
   StudentScore int
);
Query OK, 0 rows affected (0.68 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(StudentName,StudentScore) values('John',65);
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable(StudentName,StudentScore) values('Chris',98);
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable(StudentName,StudentScore) values('Robert',91);
Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+-------------+--------------+
| StudentId | StudentName | StudentScore |
+-----------+-------------+--------------+
|         1 | John        | 65           |
|         2 | Chris       | 98           |
|         3 | Robert      | 91           |
+-----------+-------------+--------------+
3 rows in set (0.00 sec)

Following is the query to add a percentage (%) sign to each value at the end while using MySQL SELECT statement −

mysql> select StudentId,StudentName,concat(StudentScore,'%') AS StudentScore from DemoTable;

This will produce the following output −

+-----------+-------------+--------------+
| StudentId | StudentName | StudentScore |
+-----------+-------------+--------------+
|         1 | John        | 65%          |
|         2 | Chris       | 98%          |
|         3 | Robert      | 91%          |
+-----------+-------------+--------------+
3 rows in set (0.00 sec)
raja
Published on 26-Sep-2019 10:26:34
Advertisements