MySQL - QUOTE() Function



The MySQL QUOTE() function accepts a string value and, adds the required characters to make it a properly escaped value and returns the result.

This function wraps the given string in single quotation marks ('), and it also escapes certain characters, such as backslash (\), single quote ('), ASCII NULL, and Control+Z, by adding an escape character.

Syntax

Following is the syntax of MySQL QUOTE() function −

QUOTE(str);

Parameters

This function takes a string value as a parameter.

Return Value

This function returns a quoted string, escaping special characters and adding quotes.

Example

In the following example, we are using the QUOTE() function to escape the provided string and wrap it in single quotes −

SELECT QUOTE('Hello ''how''are''you''welcome''to''Tutorialspoint');

Following is the output of the above code −

QUOTE('Hello ''how''are''you''welcome''to''Tutorialspoint')
'Hello \'how\'are\'you\'welcome\'to\'Tutorialspoint'

Example

In here, the QUOTE() function is used to escape characters within a string −

SELECT QUOTE('this\ is \a \sample \string');

The output obtained is as follows −

QUOTE('this\ is \a \sample \string')
'this is a sample string'

Example

If you pass NULL as an argument to the QUOTE() function, it returns NULL −

SELECT QUOTE(NULL);

The result produced is as follows −

QUOTE(NULL)
0x4E554C4C

Example

You can also add quotes to the entities of a column of a table using the QUOTE() function.

Let us create a table named "STUDENTS_TABLE" and insert records into it using CREATE and INSERT statements as shown below −

CREATE TABLE STUDENTS_TABLE (
   name VARCHAR(15),
   marks INT,
   grade CHAR
);

Now, let us insert records into it using the INSERT statement −

INSERT INTO STUDENTS_TABLE VALUES 
('Raju', 80, 'A'),
('Rahman', 60, 'B'),
('Robert', 45, 'C');

The STUDENTS_TABLE obtained is as follows −

name marks grade
Raju 80 A
Rahman 60 B
Robert 45 C

Following query add quotes to the "grade" column in the STUDENTS_TABLE −

SELECT name, marks, grade, QUOTE(grade) 
FROM STUDENTS_TABLE;

After executing the above code, we get the following output −

name marks grade QUOTE(grade)
Raju 80 A 'A'
Rahman 60 B 'B'
Robert 45 C 'C'
mysql-quote-function.htm
Advertisements