Escape sequences in MySQL
In this article, we will learn about escape sequences. We will look at its definition with examples and sample code.
We'll also explore how to use it with wildcards to find patterns in your data.
Definition of escape sequences
An escape sequence character is a non-printable character that specifies that the alternative interpretation of the following character is the escape character sequence.
It starts with a backslash (denoted as *\\*
) and has two or more characters. For example, \n
a new line is displayed where 反斜杠
is one character and n
is the second character.
Given below is a list of escape sequences and their representations.
Escape sequences | Character representation |
---|---|
\n |
Line Breaks |
\0 |
Null character |
\b |
Backspace character |
\r |
Carriage Return |
\t |
Tab |
\\ |
Backslash |
\% |
Percent character |
\a |
alarm |
\f |
Page break (new page) |
\v |
Vertical Labels |
\' |
Single quotes |
" |
Double quotes |
\? |
question mark |
When writing applications, there are situations where you have to manipulate strings. 数据库
This string must be escaped properly before saving it to .
Here we use escape sequences. For example, if you want to have records in a table customer_firstname
with , you have to use .Nyy'a
customer
INSERT
escape sequence
Customer table:
Order form:
Sample code:
INSERT INTO customer(customer_firstname, customer_lastname, customer_age, customer_salary)
VALUES
('Nyy\'a', 'Daniel', 19, 10000);
Output:
Escape sequences in MySQL
There are different escape sequences used in MySQL. See the following examples to understand.
New line example code:
SELECT 'Hi!\nWelcome to the learning Escape Sequences.'
Output:
Hi!
Welcome to the learning Escape Sequences.
Example code for carriage return character:
SET @strInput = 'Hello,How are you';
SET @strResult = REPLACE(@strInput, ',', CHAR(10)); #CHAR(10) represents \r
SELECT @strResult;
Output:
Hello
How are you
Question mark example code:
SELECT 'Is this a question mark example\?';
Output:
Is this a question mark example?
Quote sample code:
SELECT 'firstname', 'first\'name', '"firstname"',"firstname", '\"firstname"','firstname\?';
Output:
MySQL with wildcards转义序列
Wildcard characters are used to obtain the required pattern from the data and replace one or more strings.
It LIKE
is used with the operator, LIKE
which is used in WHERE
the clause. Using escape sequences with wildcard characters makes it easy to get specific patterns.
Sample code:
SELECT customer_firstname from customer where customer_firstname like '___\'_';
In this code, we customer
look up from the table customer_firstname
where there are three characters before the single quote and one character after the single quote. An underscore ( _
) is used for one character.
In ___\'_'
, we use three underscores to get three characters, followed by a single quote, and finally a character. See the following output for comparison.
Output:
What if you want to find some pattern in a column? Let's order
practice it using a table. We will find all the order dates from the field that contain -12
the pattern .order_date
One or more characters are shown here %
. It represents one or more characters before the required pattern and one or more characters in the following sample code.
Sample code:
SELECT order_date from order where order_date like '%\-12%';
Output:
To understand the wild card characters
double quotes example with , INSERT``customer
new record in the table.
Sample code:
INSERT INTO customer(customer_firstname, customer_lastname, customer_age, customer_salary)
VALUES
('"Nyy\'a\"', 'Dan\'iel', 19, 10000);
Output:
Use the following command to find and customer
from the table that match the following pattern .customer_firstname
customer_lastname
Sample code:
SELECT customer_firstname, customer_lastname from customer
where customer_firstname like '%"%\'%\"'
AND customer_lastname like'%\'___';
Output:
in conclusion
The paper concludes that an escape sequence is non-printable, it specifies an alternative representation on the following character.
Strings must be escaped before being saved to the database. We also learned that escape characters are used with wildcards to find different patterns.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
If ELSE in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering
DATETIME vs. TIMESTAMP in MySQL
Publish Date:2025/04/11 Views:117 Category:MySQL
-
DATETIME and TIMESTAMP are two different data types that can be used to store values that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea
Execute multiple joins in one query in MYSQL
Publish Date:2025/04/11 Views:94 Category:MySQL
-
Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.
Joining 3 tables in MySQL
Publish Date:2025/04/11 Views:187 Category:MySQL
-
In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS
Use of UPDATE JOIN in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla
How to use the Row_Number() function in MySQL
Publish Date:2025/04/11 Views:142 Category:MySQL
-
In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti
Multiple primary keys in MySQL
Publish Date:2025/04/11 Views:66 Category:MySQL
-
In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b
Displaying foreign keys in MySQL
Publish Date:2025/04/11 Views:55 Category:MySQL
-
In this tutorial, we aim to explore how to display foreign keys for tables and columns in MySQL. The type of key that references a primary key, also known as the primary key of another table, is called a foreign key. Understanding the forei
Select first N rows in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
Sometimes, you have to select first N rows of MySQL database according to your project requirements. n The value of varies according to the requirement; it can be TOP 1 row or TOP 30 rows. We will learn how to select top N rows using the cl