String Functions
ASCII(str) | This function returns the ASCII code value of the left-most character in str, 0 if str is an empty string, or NULL if str is NULL. | |
ORD(str) | This function returns the ASCII code of the left-most character in str, taking into account the possibility that it might be a multi-byte character. | |
CONV(expr,from_base,to_base) | This function converts a number (expr) in base from_base to a number in base to_base. Returns NULL if any of the arguments are NULL. | |
BIN(expr) | This function converts decimal expr to binary, equivalent to CONV(expr,10,2). | |
OCT(expr) | This function converts decimal expr to octal, equivalent to CONV(expr,10,8). | |
HEX(expr) | This function converts decimal expr to hexadecimal, equivalent to CONV(expr,10,16). | |
CHAR(expr,...) | This function creates a string composed of characters, the ASCII code values of which are given by the expressions passed as arguments. | |
CONCAT(str1,str2,...) | This function returns a string made up of the strings passed as arguments joined end-to-end. If any of the arguments are NULL, NULL is returned instead. | |
CONCAT_WS(separator,str1,str2,...) | CONCAT "With Separator" (WS). Same as CONCAT, except the first argument is placed between each of the additional arguments when they are combined. | |
LENGTH(str),OCTET_LENGTH(str), CHAR_LENGTH(str),CHARACTER_LENGTH(str) | All of these return the length in characters of str. CHAR_LENGTH and CHARACTER_LENGTH, however, take multi-byte characters into consideration when performing the count. | |
BIT_LENGTH(str) | This function returns the length (in bits) of str (i.e. BIT_LENGTH(str) = 8 * LENGTH(str)). | |
LOCATE(substr,str),POSITION(substr IN str) | This function returns the position of the first occurrence of substr in str (1 if it occurs at the beginning, 2 if it starts after one character, and so on). Returns 0 if substr does not occur in str. | |
LOCATE(substr,str,pos) | Same as LOCATE(substr,str), but begins searching from character number pos. | |
INSTR(str,substr) | This function is the same as LOCATE(substr,str), but with argument order swapped. | |
LPAD(str,len,padstr) | This function shortens or lengthens str so that it is of length len. Lengthening is accomplished by inserting padstr to the left of the characters of str (e.g. LPAD('!','5','.') = '....!'). | |
RPAD(str,len,padstr) | This function shortens or lengthens str so that it is of length len. Lengthening is accomplished by inserting padstr to the right of the characters of str (e.g. RPAD('!','5','.') = '!....'). | |
LEFT(str,len) | This function returns the left-most len characters of str. If str is shorter than len characters, str is returned with no extra padding. | |
RIGHT(str,len) | This function returns the right-most len characters of str. If str is shorter than len characters, str is returned with no extra padding. | |
SUBSTRING(str,pos,len), SUBSTRING(str FROM pos FOR len), MID(str,pos,len) | This function returns a string up to len characters long taken from str beginning at position pos (where 1 is the first character). The second form of SUBSTRING is the ANSI standard. | |
SUBSTRING(str,pos),SUBSTRING(str FROM pos) | This function returns the string beginning from position pos in str (where 1 is the first character) and going to the end of str. | |
SUBSTRING_INDEX(str,delim,count) | MySQL counts count occurrences of delim in str, then takes the substring from that point. If count is positive, MySQL counts to the right from the start of the string, and then takes the substring up to but not including that delimiter. If count is negative, MySQL counts to the left from the end of the string, and then takes the substring that starts right after that delimiter, and runs to the end of str. | |
LTRIM(str) | This function returns str with any leading white space trimmed off. | |
RTRIM(str) | This function returns str with any trailing white space trimmed off. | |
TRIM([[BOTH | LEADING | TRAILING] str) [remstr] FROM] | This function returns str with either white space (by default) or occurrences of the string remstr removed from the start of the string (LEADING), end of the string (TRAILING), or both (BOTH, the default). | |
SOUNDEX(str) | This function produces a string that represents how str sounds when read aloud. Words that sound similar should have the same 'soundex string'.E.g.: SOUNDEX("tire") = "T600" | |
SPACE(num) | This function returns a string of num space characters. | |
REPLACE(str,from_str,to_str) | This function returns str after replacing all occurrences of from_str to to_str. | |
REPEAT(str,count) | This function returns a string made up of str repeated count times, an empty string if count <= 0, or NULL if either argument is NULL. | |
REVERSE(str) | This function returns str spelled backwards. | |
INSERT(str,pos,len,newstr) | This function takes str, and removes the substring beginning at pos (where 1 is the first character in the string) with length len, then inserts newstr at that position. If len = 0, simply inserts newstr at position pos. | |
ELT(N,str1,str2,str3,...) | This function returns the Nth string argument (str1 if N=1, str2 if N=2 and so on), or NULL if there is no argument for the given N. | |
FIELD(str,str1,str2,str3,...) | This function returns the position of str in the subsequent list of arguments (1 if str = str1, 2 if str = str2, and so on). | |
FIND_IN_SET(str,strlist) | When strlist is a list of strings of the form 'string1,string2,string3,...' this function returns the index of str in that list, or 0 if str is not in the list. This function is ideally suited (and optimized) for determining if str is selected in a column of type SET (see "MySQL Column Types"). | |
MAKE_SET(bits,str1,str2,...) | This function returns a list of strings of the form 'string1,string2,string3,...' using the string parameters (str1, str2, etc.) that correspond to the bits that are set in the number bits. For example, if bits = 10 (binary 1010) then bits 2 and 4 are set, so the output of MAKE_SET will be 'str2,str4'. | |
EXPORT_SET(bits,on_str, off_str[, separator[, number_of_bits]]) | This function returns a string representation of which bits are, and are not set in bits. Set bits are represented by the on_str string, while unset bits are represented by the off_str string. By default, these bit representations are comma-separated, but the optional separator string lets you define your own. By default, up to 64 bits of bits are read; however, number_of_bits lets you specify that a smaller number be read.E.g.: EXPORT_SET(10,'Y','N',',',6) = 'N,Y,N,Y,N,N' | |
LCASE(str),LOWER(str) | This function returns str with all letters in lowercase. | |
UCASE(str),UPPER(str) | This function returns str with all letters in uppercase. | |
LOAD_FILE(filename) | This function returns the contents of the file specified by filename (an absolute path to a file readable by MySQL). Your MySQL user should also have file privileges. |