12.3. String Functions

MySQL 5.0

12.3. String Functions

String-valued functions return if the length of the result would be greater than the value of the system variable. See Section 7.5.2, “Tuning Server Parameters”.

For functions that operate on string positions, the first position is numbered 1.

  • )

    Returns the numeric value of the leftmost character of the string . Returns if is the empty string. Returns if is . works for characters with numeric values from to .

    mysql> 
            -> 50
    mysql> 
            -> 50
    mysql> 
            -> 100
    

    See also the function.

  • )

    Returns a string representation of the binary value of , where is a longlong () number. This is equivalent to ,10,2). Returns if is .

    mysql> 
            -> '1100'
    
  • )

    Returns the length of the string in bits.

    mysql> 
            -> 32
    
  • ,... [USING ])

    interprets each argument as an integer and returns a string consisting of the characters given by the code values of those integers. values are skipped.

    mysql> 
            -> 'MySQL'
    mysql> 
            -> 'MMM'
    

    As of MySQL 5.0.15, arguments larger than 255 are converted into multiple result bytes. For example, is equivalent to , and is equivalent to :

    mysql> 
    +----------------+----------------+
    | HEX(CHAR(1,0)) | HEX(CHAR(256)) |
    +----------------+----------------+
    | 0100           | 0100           |
    +----------------+----------------+
    mysql> 
    +------------------+--------------------+
    | HEX(CHAR(1,0,0)) | HEX(CHAR(256*256)) |
    +------------------+--------------------+
    | 010000           | 010000             |
    +------------------+--------------------+
    

    By default, returns a binary string. To produce a string in a given character set, use the optional clause:

    mysql> 
    +---------------------+--------------------------------+
    | CHARSET(CHAR(0x65)) | CHARSET(CHAR(0x65 USING utf8)) |
    +---------------------+--------------------------------+
    | binary              | utf8                           |
    +---------------------+--------------------------------+
    

    If 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 from becomes .

    Before MySQL 5.0.15, returns a string in the connection character set and the clause is unavailable. In addition, each argument is interpreted modulo 256, so and both are equivalent to .

  • )

    Returns the length of the string , measured in characters. A multi-byte character counts as a single character. This means that for a string containing five two-byte characters, returns , whereas returns .

  • )

    is a synonym for .

  • ,,...)

    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( AS CHAR), );
    

    returns if any argument is .

    mysql> 
            -> 'MySQL'
    mysql> 
            -> NULL
    mysql> 
            -> '14.3'
    
  • ,,,...)

    stands for Concatenate With Separator and is a special form of . 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 is , the result is .

    mysql> 
            -> 'First name,Second name,Last Name'
    mysql> 
            -> 'First name,Last Name'
    

    does not skip empty strings. However, it does skip any values after the separator argument.

  • ,,)

    Converts numbers between different number bases. Returns a string representation of the number , converted from base to base . Returns if any argument is . The argument is interpreted as an integer, but may be specified as an integer or a string. The minimum base is and the maximum base is . If is a negative number, is regarded as a signed number. Otherwise, is treated as unsigned. works with 64-bit precision.

    mysql> 
            -> '1010'
    mysql> 
            -> '172'
    mysql> 
            -> '-H'
    mysql> 
            -> '40'
    
  • ,,,,...)

    Returns if = , if = , and so on. Returns if is less than or greater than the number of arguments. is the complement of .

    mysql> 
            -> 'ej'
    mysql> 
            -> 'foo'
    
  • ,,[,[,]])

    Returns a string such that for every bit set in the value , you get an string and for every reset bit, you get an string. Bits in 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 the string (the default being the comma character ‘’). The number of bits examined is given by (defaults to 64).

    mysql> 
            -> 'Y,N,Y,N'
    mysql> 
            -> '0,1,1,0,0,0,0,0,0,0'
    
  • ,,,,...)

    Returns the index (position) of in the , , , list. Returns if is not found.

    If all arguments to 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 is , the return value is because fails equality comparison with any value. is the complement of .

    mysql> 
            -> 2
    mysql> 
            -> 0
    
  • ,)

    Returns a value in the range of 1 to if the string is in the string list consisting of 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 type , the function is optimized to use bit arithmetic. Returns if is not in or if is the empty string. Returns if either argument is . This function does not work properly if the first argument contains a comma (‘’) character.

    mysql> 
            -> 2
    
  • ,)

    Formats the number to a format like , rounded to decimal places, and returns the result as a string. If is , the result has no decimal point or fractional part.

    mysql> 
            -> '12,332.1235'
    mysql> 
            -> '12,332.1000'
    mysql> 
            -> '12,332'
    
  • )

    If is a number, returns a string representation of the hexadecimal value of , where is a longlong () number. This is equivalent to ,10,16).

    If is a string, returns a hexadecimal string representation of where each character in is converted to two hexadecimal digits.

    mysql> 
            -> 'FF'
    mysql> 
            -> 'abc'
    mysql> 
            -> 616263
    
  • ,,,)

    Returns the string , with the substring beginning at position and characters long replaced by the string . Returns the original string if is not within the length of the string. Replaces the rest of the string from position is is not within the length of the rest of the string. Returns if any argument is .

    mysql> 
            -> 'QuWhattic'
    mysql> 
            -> 'Quadratic'
    mysql> 
            -> 'QuWhat'
    

    This function is multi-byte safe.

  • ,)

    Returns the position of the first occurrence of substring in string . This is the same as the two-argument form of , except that the order of the arguments is reversed.

    mysql> 
            -> 4
    mysql> 
            -> 0
    

    This function is multi-byte safe, and is case sensitive only if at least one argument is a binary string.

  • )

    is a synonym for .

  • ,)

    Returns the leftmost characters from the string , or if any argument is .

    mysql> 
            -> 'fooba'
    
  • )

    Returns the length of the string , measured in bytes. A multi-byte character counts as multiple bytes. This means that for a string containing five two-byte characters, returns , whereas returns .

    mysql> 
            -> 4
    
  • )

    Reads the file and returns the file contents as a string. To use this function, the file must be located on the server host, you must specify the full pathname to the file, and you must have the privilege. The file must be readable by all and its size less than bytes.

    If the file does not exist or cannot be read because one of the preceding conditions is not satisfied, the function returns .

    As of MySQL 5.0.19, the system variable controls interpretation of filenames that are given as literal strings.

    mysql> 
              
              
    
  • ,), ,,)

    The first syntax returns the position of the first occurrence of substring in string . The second syntax returns the position of the first occurrence of substring in string , starting at position . Returns if is not in .

    mysql> 
            -> 4
    mysql> 
            -> 0
    mysql> 
            -> 7
    

    This function is multi-byte safe, and is case-sensitive only if at least one argument is a binary string.

  • )

    Returns the string with all characters changed to lowercase according to the current character set mapping. The default is (cp1252 West European).

    mysql> 
            -> 'quadratically'
    

    This function is multi-byte safe.

  • ,,)

    Returns the string , left-padded with the string to a length of characters. If is longer than , the return value is shortened to characters.

    mysql> 
            -> '??hi'
    mysql> 
            -> 'h'
    
  • )

    Returns the string with leading space characters removed.

    mysql> 
            -> 'barbar'
    

    This function is multi-byte safe.

  • ,,,...)

    Returns a set value (a string containing substrings separated by ‘’ characters) consisting of the strings that have the corresponding bit in set. corresponds to bit 0, to bit 1, and so on. values in , , are not appended to the result.

    mysql> 
            -> 'a'
    mysql> 
            -> 'hello,world'
    mysql> 
            -> 'hello'
    mysql> 
            -> ''
    
  • ,,)

    ,,) is a synonym for ,,).

  • )

    Returns a string representation of the octal value of , where is a longlong () number. This is equivalent to ,10,8). Returns if is .

    mysql> 
            -> '14'
    
  • )

    is a synonym for .

  • )

    If the leftmost character of the string is a multi-byte character, returns the code for that character, calculated from the numeric values of its constituent bytes using this formula:

      (1st byte code)
    + (2nd byte code × 256)
    + (3rd byte code × 2562) ...
    

    If the leftmost character is not a multi-byte character, returns the same value as the function.

    mysql> 
            -> 50
    
  • IN )

    IN ) is a synonym for ,).

  • )

    Quotes a string to produce a result that can be used as a properly escaped data value in an SQL statement. The string is returned enclosed by single quotes and with each instance of single quote (‘’), backslash (‘’), ASCII , and Control-Z preceded by a backslash. If the argument is , the return value is the word “NULL” without enclosing single quotes.

    mysql> 
            -> 'Don\'t!'
    mysql> 
            -> NULL
    
  • ,)

    Returns a string consisting of the string repeated times. If is less than 1, returns an empty string. Returns if or are .

    mysql> 
            -> 'MySQLMySQLMySQL'
    
  • ,,)

    Returns the string with all occurrences of the string replaced by the string . performs a case-sensitive match when searching for .

    mysql> 
            -> 'WwWwWw.mysql.com'
    

    This function is multi-byte safe.

  • )

    Returns the string with the order of the characters reversed.

    mysql> 
            -> 'cba'
    

    This function is multi-byte safe.

  • ,)

    Returns the rightmost characters from the string , or if any argument is .

    mysql> 
            -> 'rbar'
    

    This function is multi-byte safe.

  • ,,)

    Returns the string , right-padded with the string to a length of characters. If is longer than , the return value is shortened to characters.

    mysql> 
            -> 'hi???'
    mysql> 
            -> 'h'
    

    This function is multi-byte safe.

  • )

    Returns the string with trailing space characters removed.

    mysql> 
            -> 'barbar'
    

    This function is multi-byte safe.

  • )

    Returns a soundex string from . Two strings that sound almost the same should have identical soundex strings. A standard soundex string is four characters long, but the function returns an arbitrarily long string. You can use on the result to get a standard soundex string. All non-alphabetic characters in are ignored. All international alphabetic characters outside the A-Z range are treated as vowels.

    mysql> 
            -> 'H400'
    mysql> 
            -> 'Q36324'
    

    Note: This function implements the original Soundex algorithm, not the more popular enhanced version (also described by D. Knuth). The difference is that original version discards vowels first and duplicates second, whereas the enhanced version discards duplicates first and vowels second.

  • SOUNDS LIKE

    This is the same as ) = SOUNDEX().

  • )

    Returns a string consisting of space characters.

    mysql> 
            -> '      '
    
  • ,), FROM ), ,,), FROM FOR )

    The forms without a argument return a substring from string starting at position . The forms with a argument return a substring characters long from string , starting at position . The forms that use are standard SQL syntax. It is also possible to use a negative value for . In this case, the beginning of the substring is characters from the end of the string, rather than the beginning. A negative value may be used for in any of the forms of this function.

    mysql> 
            -> 'ratically'
    mysql> 
            -> 'barbar'
    mysql> 
            -> 'ratica'        
    mysql> 
            -> 'ila'        
    mysql> 
            -> 'aki'
    mysql> 
            -> 'ki'
    

    This function is multi-byte safe.

    If is less than 1, the result is the empty string.

    is a synonym for .

  • ,,)

    Returns the substring from string before occurrences of the delimiter . If is positive, everything to the left of the final delimiter (counting from the left) is returned. If is negative, everything to the right of the final delimiter (counting from the right) is returned. performs a case-sensitive match when searching for .

    mysql> 
            -> 'www.mysql'
    mysql> 
            -> 'mysql.com'
    

    This function is multi-byte safe.

  • ] FROM] ), FROM] )

    Returns the string with all prefixes or suffixes removed. If none of the specifiers , , or is given, is assumed. is optional and, if not specified, spaces are removed.

    mysql> 
            -> 'bar'
    mysql> 
            -> 'barxxx'
    mysql> 
            -> 'bar'
    mysql> 
            -> 'barx'
    

    This function is multi-byte safe.

  • )

    is a synonym for .

  • )

    Performs the inverse operation of ). That is, it interprets each pair of hexadecimal digits in the argument as a number and converts it to the character represented by the number. The resulting characters are returned as a binary string.

    mysql> 
            -> 'MySQL'
    mysql> 
            -> 'MySQL'
    mysql> 
            -> 'string'
    mysql> 
            -> '1267'
    
  • )

    Returns the string with all characters changed to uppercase according to the current character set mapping. The default is (cp1252 West European).

    mysql> 
            -> 'HEJ'
    

    This function is multi-byte safe.

12.3.1. String Comparison Functions

If a string function is given a binary string as an argument, the resulting string is also a binary string. A number converted to a string is treated as a binary string. This affects only comparisons.

Normally, if any expression in a string comparison is case sensitive, the comparison is performed in case-sensitive fashion.

  • LIKE [ESCAPE '']

    Pattern matching using SQL simple regular expression comparison. Returns () or (). If either or is , the result is .

    The pattern need not be a literal string. For example, it can be specified as a string expression or table column.

    Per the SQL standard, performs matching on a per-character basis, thus it can produce results different from the comparison operator:

    mysql> 
    +-----------------------------------------+
    | 'ä' LIKE 'ae' COLLATE latin1_german2_ci |
    +-----------------------------------------+
    |                                       0 |
    +-----------------------------------------+
    mysql> 
    +--------------------------------------+
    | 'ä' = 'ae' COLLATE latin1_german2_ci |
    +--------------------------------------+
    |                                    1 |
    +--------------------------------------+
    

    With you can use the following two wildcard characters in the pattern:

    Character Description
    Matches any number of characters, even zero characters
    Matches exactly one character
    mysql> 
            -> 1
    mysql> 
            -> 1
    

    To test for literal instances of a wildcard character, precede it by the escape character. If you do not specify the character, ‘’ is assumed.

    String Description
    Matches one ‘’ character
    Matches one ‘’ character
    mysql> 
            -> 0
    mysql> 
            -> 1
    

    To specify a different escape character, use the clause:

    mysql> 
            -> 1
    

    The escape sequence should be empty or one character long. As of MySQL 5.0.16, if the SQL mode is enabled, the sequence cannot be empty.

    The following two statements illustrate that string comparisons are not case sensitive unless one of the operands is a binary string:

    mysql> 
            -> 1
    mysql> 
            -> 0
    

    In MySQL, is allowed on numeric expressions. (This is an extension to the standard SQL .)

    mysql> 
            -> 1
    

    Note: Because MySQL uses C escape syntax in strings (for example, ‘’ to represent a newline character), you must double any ‘’ that you use in strings. For example, to search for ‘’, specify it as ‘’. To search for ‘’, specify it as ‘’; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against. (Exception: At the end of the pattern string, backslash can be specified as ‘’. At the end of the string, backslash stands for itself because there is nothing following to escape.)

  • NOT LIKE [ESCAPE '']

    This is the same as LIKE [ESCAPE '']).

  • NOT REGEXP , NOT RLIKE

    This is the same as REGEXP ).

  • REGEXP RLIKE

    Performs a pattern match of a string expression against a pattern . The pattern can be an extended regular expression. The syntax for regular expressions is discussed in Appendix G, Regular Expressions. Returns if matches ; otherwise it returns . If either or is , the result is . is a synonym for , provided for compatibility.

    The pattern need not be a literal string. For example, it can be specified as a string expression or table column.

    Note: Because MySQL uses the C escape syntax in strings (for example, ‘’ to represent the newline character), you must double any ‘’ that you use in your strings.

    is not case sensitive, except when used with binary strings.

    mysql> 
            -> 0
    mysql> 
            -> 1
    mysql> 
            -> 1
    mysql> 
            -> 1  0
    mysql> 
            -> 1
    

    and use the current character set when deciding the type of a character. The default is (cp1252 West European). Warning: These operators are not multi-byte safe.

  • ,)

    returns if the strings are the same, if the first argument is smaller than the second according to the current sort order, and otherwise.

    mysql> 
            -> -1
    mysql> 
            -> 1
    mysql> 
            -> 0
    

    uses the current character set when performing comparisons. This makes the default comparison behavior case insensitive unless one or both of the operands are binary strings.