String Functions
Name | Description |
---|---|
ASCII() |
Return numeric value of left-most character |
BIN() |
Return a string representation of the argument |
BIT_LENGTH() |
Return length of argument in bits |
CHAR_LENGTH() |
Return number of characters in argument |
CHAR() |
Return the character for each integer passed |
CHARACTER_LENGTH() |
A synonym for CHAR_LENGTH() |
CONCAT_WS() |
Return concatenate with separator |
CONCAT() |
Return concatenated string |
ELT() |
Return string at index number |
EXPORT_SET() |
Return a string such that for every bit set in the value bits, you get an on string and for every unset bit, you get an off string |
FIELD() |
Return the index (position) of the first argument in the subsequent arguments |
FIND_IN_SET() |
Return the index position of the first argument within the second argument |
FORMAT() |
Return a number formatted to specified number of decimal places |
HEX() |
Return a hexadecimal representation of a decimal or string value |
INSERT() |
Insert a substring at the specified position up to the specified number of characters |
INSTR() |
Return the index of the first occurrence of substring |
LCASE() |
Synonym for LOWER() |
LEFT() |
Return the leftmost number of characters as specified |
LENGTH() |
Return the length of a string in bytes |
LIKE |
Simple pattern matching |
LOAD_FILE() |
Load the named file |
LOCATE() |
Return the position of the first occurrence of substring |
LOWER() |
Return the argument in lowercase |
LPAD() |
Return the string argument, left-padded with the specified string |
LTRIM() |
Remove leading spaces |
MAKE_SET() |
Return a set of comma-separated strings that have the corresponding bit in bits set |
MATCH |
Perform full-text search |
MID() |
Return a substring starting from the specified position |
NOT LIKE |
Negation of simple pattern matching |
NOT REGEXP |
Negation of REGEXP |
OCTET_LENGTH() |
A synonym for LENGTH() |
ORD() |
If the leftmost character of the argument is a multi-byte character, returns the code for that character |
POSITION() |
A synonym for LOCATE() |
QUOTE() |
Escape the argument for use in an SQL statement |
REGEXP |
Pattern matching using regular expressions |
REPEAT() |
Repeat a string the specified number of times |
REPLACE() |
Replace occurrences of a specified string |
REVERSE() |
Reverse the characters in a string |
RIGHT() |
Return the specified rightmost number of characters |
RLIKE |
Synonym for REGEXP |
RPAD() |
Append string the specified number of times |
RTRIM() |
Remove trailing spaces |
SOUNDEX() |
Return a soundex string |
SOUNDS LIKE (v4.1.0) |
Compare sounds |
SPACE() |
Return a string of the specified number of spaces |
STRCMP() |
Compare two strings |
SUBSTR() |
Return the substring as specified |
SUBSTRING_INDEX() |
Return a substring from a string before the specified number of occurrences of the delimiter |
SUBSTRING() |
Return the substring as specified |
TRIM() |
Remove leading and trailing spaces |
UCASE() |
Synonym for UPPER() |
UNHEX() (v4.1.2) |
Convert each pair of hexadecimal digits to a character |
UPPER() |
Convert to uppercase |
String-valued functions return NULL
if the length of the result would be greater than the value of the max_allowed_packet
system variable. See Section 6.5.2, “Tuning Server Parameters”.
For functions that operate on string positions, the first position is numbered 1.
For functions that take length arguments, non-integer arguments are rounded to the nearest integer.
ASCII(
str
)Returns the numeric value of the leftmost character of the string
str
. Returns0
ifstr
is the empty string. ReturnsNULL
ifstr
isNULL
.ASCII()
works for 8-bit characters.mysql>
SELECT ASCII('2');
-> 50 mysql>SELECT ASCII(2);
-> 50 mysql>SELECT ASCII('dx');
-> 100See also the
ORD()
function.BIN(
N
)Returns a string representation of the binary value of
N
, whereN
is a longlong (BIGINT
) number. This is equivalent toCONV(
. ReturnsN
,10,2)NULL
ifN
isNULL
.mysql>
SELECT BIN(12);
-> '1100'BIT_LENGTH(
str
)Returns the length of the string
str
in bits.mysql>
SELECT BIT_LENGTH('text');
-> 32CHAR(
N
,... [USINGcharset_name
])CHAR()
interprets each argumentN
as an integer and returns a string consisting of the characters given by the code values of those integers.NULL
values are skipped.mysql>
SELECT CHAR(77,121,83,81,'76');
-> 'MySQL' mysql>SELECT CHAR(77,77.3,'77.3');
-> 'MMM'As of MySQL 5.0.15,
CHAR()
arguments larger than 255 are converted into multiple result bytes. For example,CHAR(256)
is equivalent toCHAR(1,0)
, andCHAR(256*256)
is equivalent toCHAR(1,0,0)
:mysql>
SELECT HEX(CHAR(1,0)), HEX(CHAR(256));
+----------------+----------------+ | HEX(CHAR(1,0)) | HEX(CHAR(256)) | +----------------+----------------+ | 0100 | 0100 | +----------------+----------------+ mysql>SELECT HEX(CHAR(1,0,0)), HEX(CHAR(256*256));
+------------------+--------------------+ | HEX(CHAR(1,0,0)) | HEX(CHAR(256*256)) | +------------------+--------------------+ | 010000 | 010000 | +------------------+--------------------+By default,
CHAR()
returns a binary string. To produce a string in a given character set, use the optionalUSING
clause:mysql>
SELECT CHARSET(CHAR(0x65)), CHARSET(CHAR(0x65 USING utf8));
+---------------------+--------------------------------+ | CHARSET(CHAR(0x65)) | CHARSET(CHAR(0x65 USING utf8)) | +---------------------+--------------------------------+ | binary | utf8 | +---------------------+--------------------------------+If
USING
is given and the result string is illegal for the given character set, a warning is issued. Also, if strict SQL mode is enabled, the result fromCHAR()
becomesNULL
.Before MySQL 5.0.15,
CHAR()
returns a string in the connection character set and theUSING
clause is unavailable. In addition, each argument is interpreted modulo 256, soCHAR(256)
andCHAR(256*256)
both are equivalent toCHAR(0)
.CHAR_LENGTH(
str
)Returns the length of the string
str
, measured in characters. A multi-byte character counts as a single character. This means that for a string containing five two-byte characters,LENGTH()
returns10
, whereasCHAR_LENGTH()
returns5
.CHARACTER_LENGTH(
str
)CHARACTER_LENGTH()
is a synonym forCHAR_LENGTH()
.CONCAT(
str1
,str2
,...)Returns the string that results from concatenating the arguments. May have one or more arguments. If all arguments are non-binary strings, the result is a non-binary string. If the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent binary string form; if you want to avoid that, you can use an explicit type cast, as in this example:
SELECT CONCAT(CAST(
int_col
AS CHAR),char_col
);CONCAT()
returnsNULL
if any argument isNULL
.mysql>
SELECT CONCAT('My', 'S', 'QL');
-> 'MySQL' mysql>SELECT CONCAT('My', NULL, 'QL');
-> NULL mysql>SELECT CONCAT(14.3);
-> '14.3'CONCAT_WS(
separator
,str1
,str2
,...)CONCAT_WS()
stands for Concatenate With Separator and is a special form ofCONCAT()
. The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator isNULL
, the result isNULL
.mysql>
SELECT CONCAT_WS(',','First name','Second name','Last Name');
-> 'First name,Second name,Last Name' mysql>SELECT CONCAT_WS(',','First name',NULL,'Last Name');
-> 'First name,Last Name'CONCAT_WS()
does not skip empty strings. However, it does skip anyNULL
values after the separator argument.ELT(
N
,str1
,str2
,str3
,...)Returns
str1
ifN
=1
,str2
ifN
=2
, and so on. ReturnsNULL
ifN
is less than1
or greater than the number of arguments.ELT()
is the complement ofFIELD()
.mysql>
SELECT ELT(1, 'ej', 'Heja', 'hej', 'foo');
-> 'ej' mysql>SELECT ELT(4, 'ej', 'Heja', 'hej', 'foo');
-> 'foo'EXPORT_SET(
bits
,on
,off
[,separator
[,number_of_bits
]])Returns a string such that for every bit set in the value
bits
, you get anon
string and for every bit not set in the value, you get anoff
string. Bits inbits
are examined from right to left (from low-order to high-order bits). Strings are added to the result from left to right, separated by theseparator
string (the default being the comma character “,
”). The number of bits examined is given bynumber_of_bits
(defaults to 64).mysql>
SELECT EXPORT_SET(5,'Y','N',',',4);
-> 'Y,N,Y,N' mysql>SELECT EXPORT_SET(6,'1','0',',',10);
-> '0,1,1,0,0,0,0,0,0,0'FIELD(
str
,str1
,str2
,str3
,...)Returns the index (position) of
str
in thestr1
,str2
,str3
,...
list. Returns0
ifstr
is not found.If all arguments to
FIELD()
are strings, all arguments are compared as strings. If all arguments are numbers, they are compared as numbers. Otherwise, the arguments are compared as double.If
str
isNULL
, the return value is0
becauseNULL
fails equality comparison with any value.FIELD()
is the complement ofELT()
.mysql>
SELECT FIELD('ej', 'Hej', 'ej', 'Heja', 'hej', 'foo');
-> 2 mysql>SELECT FIELD('fo', 'Hej', 'ej', 'Heja', 'hej', 'foo');
-> 0FIND_IN_SET(
str
,strlist
)Returns a value in the range of 1 to
N
if the stringstr
is in the string liststrlist
consisting ofN
substrings. A string list is a string composed of substrings separated by “,
” characters. If the first argument is a constant string and the second is a column of typeSET
, theFIND_IN_SET()
function is optimized to use bit arithmetic. Returns0
ifstr
is not instrlist
or ifstrlist
is the empty string. ReturnsNULL
if either argument isNULL
. This function does not work properly if the first argument contains a comma (“,
”) character.mysql>
SELECT FIND_IN_SET('b','a,b,c,d');
-> 2FORMAT(
X
,D
)Formats the number
X
to a format like'#,###,###.##'
, rounded toD
decimal places, and returns the result as a string. IfD
is0
, the result has no decimal point or fractional part.mysql>
SELECT FORMAT(12332.123456, 4);
-> '12,332.1235' mysql>SELECT FORMAT(12332.1,4);
-> '12,332.1000' mysql>SELECT FORMAT(12332.2,0);
-> '12,332'HEX(
N_or_S
)If
N_or_S
is a number, returns a string representation of the hexadecimal value ofN
, whereN
is a longlong (BIGINT
) number. This is equivalent toCONV(
.N
,10,16)If
N_or_S
is a string, returns a hexadecimal string representation ofN_or_S
where each character inN_or_S
is converted to two hexadecimal digits. The inverse of this operation is performed by theUNHEX()
function.mysql>
SELECT HEX(255);
-> 'FF' mysql>SELECT 0x616263;
-> 'abc' mysql>SELECT HEX('abc');
-> 616263INSERT(
str
,pos
,len
,newstr
)Returns the string
str
, with the substring beginning at positionpos
andlen
characters long replaced by the stringnewstr
. Returns the original string ifpos
is not within the length of the string. Replaces the rest of the string from positionpos
iflen
is not within the length of the rest of the string. ReturnsNULL
if any argument isNULL
.mysql>
SELECT INSERT('Quadratic', 3, 4, 'What');
-> 'QuWhattic' mysql>SELECT INSERT('Quadratic', -1, 4, 'What');
-> 'Quadratic' mysql>SELECT INSERT('Quadratic', 3, 100, 'What');
-> 'QuWhat'This function is multi-byte safe.