MySQL Function to Append Values of a Column with Single Quotes



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.

Updated on: 2019-07-30T22:30:21+05:30

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements