MySQL Reference Manual for version 4.0.18.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.1 Strings

A string is a sequence of characters, surrounded by either single quote (`'') or double quote. Examples:

 
'a string'
"another string"

If the server SQL mode has ANSI_QUOTES enabled, string literals can be quoted only with single quotes. (A string quoted with double quotes will be interpreted as an identifier.)

Within a string, certain sequences have special meaning. Each of these sequences begins with a backslash (`\'), known as the escape character. MySQL recognizes the following escape sequences:

\0
An ASCII 0 (NUL) character.

\'
A single quote (`'') character.

\"
A double quote (`"') character.

\b
A backspace character.

\n
A newline (linefeed) character.

\r
A carriage return character.

\t
A tab character.

\z
ASCII 26 (Control-Z). This character can be encoded as `\z' to allow you to work around the problem that ASCII 26 stands for END-OF-FILE on Windows. (ASCII 26 will cause problems if you try to use mysql db_name < file_name.)

\\
A backslash (`\') character.

\%
A `%' character. This is used to search for literal instances of `%' in pattern-matching contexts where `%' would otherwise be interpreted as a wildcard character. See section 12.2.1 String Comparison Functions.

\_
A `_' character. This is used to search for literal instances of `_' in pattern-matching contexts where `_' would otherwise be interpreted as a wildcard character. See section 12.2.1 String Comparison Functions.

These sequences are case sensitive. For example, `\b' is interpreted as a backslash, but `\B' is interpreted as `B'.

Note that if you use `\%' or `\_' in some string contexts, these will return the strings `\%' and `\_' and not `%' and `_'.

There are several ways to include quotes within a string:

The SELECT statements shown here demonstrate how quoting and escaping work:

 
mysql> SELECT 'hello', '"hello"', '""hello""', 'hel''lo', '\'hello';
+-------+---------+-----------+--------+--------+
| hello | "hello" | ""hello"" | hel'lo | 'hello |
+-------+---------+-----------+--------+--------+

mysql> SELECT "hello", "'hello'", "''hello''", "hel""lo", "\"hello";
+-------+---------+-----------+--------+--------+
| hello | 'hello' | ''hello'' | hel"lo | "hello |
+-------+---------+-----------+--------+--------+

mysql> SELECT "This\nIs\nFour\nlines";
+--------------------+
| This
Is
Four
lines |
+--------------------+

If you want to insert binary data into a string column (such as a BLOB), the following characters must be represented by escape sequences:

NUL
NUL byte (ASCII 0). Represent this character by `\0' (a backslash followed by an ASCII `0' character).
\
Backslash (ASCII 92). Represent this character by `\\'.
'
Single quote (ASCII 39). Represent this character by `\''.
"
Double quote (ASCII 34). Represent this character by `\"'.

When writing application programs, any string that might contain any of these special characters must be properly escaped before the string is used as a data value in a SQL statement that is sent to the MySQL server. You can do this in two ways:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by rdg (Feb 25 2004) using texi2html