Which MySQL function can be used to append values of a column with single quotes?

MySQLMySQLi Database

MySQL QUOTE() function can be used to append values of a column with single quotes. For this, we must have to pass column name as the argument of QUOTE() function. Data from ‘Student’ table is used to demonstrate it as follows

Example

mysql> Select Name, ID, QUOTE(Subject)AS Subject from Student;
+---------+------+-------------+
| Name    | ID   | Subject     |
+---------+------+-------------+
| Gaurav  | 1    | 'Computers' |
| Aarav   | 2    | 'History'   |
| Harshit | 15   | 'Commerce'  |
| Gaurav  | 20   | 'Computers' |
| Yashraj | 21   | 'Math'      |
+---------+------+-------------+
5 rows in set (0.00 sec)

In contrast, it can also be done with the help of CONCAT() function as follows −

mysql> Select Name, ID, CONCAT('''',Subject,'''')AS Subject from Student;
+---------+------+-------------+
| Name    | ID   | Subject     |
+---------+------+-------------+
| Gaurav  | 1    | 'Computers' |
| Aarav   | 2    | 'History'   |
| Harshit | 15   | 'Commerce'  |
| Gaurav  | 20   | 'Computers' |
| Yashraj | 21   | 'Math'      |
+---------+------+-------------+
5 rows in set (0.00 sec)

For this purpose QUOTE() function is very easy to use.

raja
Published on 20-Feb-2018 10:37:02
Advertisements