MariaDB String Functions Guide
This guide goes through several built-in string functions in MariaDB, grouping them by similar features, and providing examples of how they might be used.
This guide explores a variety of MariaDB's built-in string functions essential for effective data manipulation. Learn how to format text for display, extract specific substrings, replace content, and utilize various expression aids to enhance your string operations in SQL queries.
Formatting Strings
Several functions are available for formatting text and numbers for display or processing.
Concatenating Strings:
CONCAT(str1, str2, ...): Joins two or more strings together.SQL
SELECT CONCAT(name_first, ' ', name_last) AS Name FROM contacts;This displays a full name by combining
name_first, a space, andname_last.CONCAT_WS(separator, str1, str2, ...): Joins strings with a specified separator between each.SQL
SELECT CONCAT_WS('|', col1, col2, col3) FROM table1;This creates a pipe-delimited string from
col1,col2, andcol3.
Formatting Numbers:
FORMAT(number, decimal_places): Formats a number with commas every three digits and a specified number of decimal places.SQLSELECT CONCAT('$', FORMAT(col5, 2)) AS Price FROM table3;This prepends a dollar sign to a number formatted with commas and two decimal places (e.g.,
$100,000.00).
Changing Case:
UCASE(str)orUPPER(str): Converts a string to all upper-case letters.LCASE(str)orLOWER(str): Converts a string to all lower-case letters.SQL
Padding Strings:
LPAD(str, len, padstr): Left-padsstrwithpadstruntil it islencharacters long.RPAD(str, len, padstr): Right-padsstrwithpadstruntil it islencharacters long.SQLExample:
RPAD('H200', 8, '.')might produceH200.....LPAD('hinge', 15, '_')might produce__________hinge.
Trimming Strings:
LTRIM(str): Removes leading spaces.RTRIM(str): Removes trailing spaces.TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str): Removes leading, trailing, or both occurrences ofremstr(or spaces ifremstris not given).BOTHis the default if no specifier is given beforeremstr. If onlystris provided, trims leading and trailing spaces.
Extracting Substrings
These functions help extract specific parts of a string.
LEFT(str, len): Returns the leftmostlencharacters fromstr.RIGHT(str, len): Returns the rightmostlencharacters fromstr.This extracts the first 3 characters as
area_codeand the last 7 astel_nbr.SUBSTRING(str, pos, [len])orMID(str, pos, [len]): Returns a substringlencharacters long fromstr, starting at positionpos.MID()is a synonym forSUBSTRING(). Iflenis omitted, returns the rest of the string frompos.This formats a 10-digit phone number like
(504) 555-1234.
Manipulating Strings
Functions for changing or generating strings.
REPLACE(str, from_str, to_str): Replaces all occurrences offrom_strwithinstrwithto_str.This replaces "Mrs." with "Ms." in the
titlecolumn.INSERT(str, pos, len, newstr): Replaces the substring instrstarting atposandlencharacters long withnewstr. Iflenis 0,newstris inserted atposwithout overwriting.LOCATE(substr, str, [pos]): Returns the starting position of the first occurrence ofsubstrwithinstr. An optionalposspecifies where to start searching. Returns 0 ifsubstris not found.This finds 'Mrs.' in the
namestring, and replaces it with 'Ms.'.LENGTH('Mrs.')(which is 4) is used forlen. IfLOCATE()returns 0,INSERT()with a position of 0 typically returns the original string unchanged.REVERSE(str): Reverses the characters instr.REPEAT(str, count): Repeatsstrcounttimes.
String Expression Aids
Functions that provide information about strings or assist in specific comparisons/conversions.
CHAR_LENGTH(str)orCHARACTER_LENGTH(str): Returns the length ofstrin characters.This counts rows where
school_idhas exactly 8 characters.INET_ATON(ip_address_str): Converts an IPv4 address string (e.g., '10.0.1.1') into a numeric representation suitable for numeric sorting.INET_NTOA(numeric_ip_representation): Converts the numeric representation back to an IPv4 address string.
To correctly sort IP addresses numerically instead of lexically:
Lexical sort of 10.0.1.1, 10.0.11.1, 10.0.2.1 might be 10.0.1.1, 10.0.11.1, 10.0.2.1.
Numeric sort (using INET_ATON) would correctly be 10.0.1.1, 10.0.2.1, 10.0.11.1.
STRCMP(str1, str2): Performs a case-sensitive comparison ofstr1andstr2.Returns
0if strings are identical.Returns
-1ifstr1is alphabetically beforestr2.Returns
1ifstr1is alphabetically afterstr2.
SUBSTRING_INDEX(str, delim, count): Returns a substring fromstrbefore or aftercountoccurrences of the delimiterdelim.If
countis positive, returns everything to the left of thecount-th delimiter (from the left).If
countis negative, returns everything to the right of theabs(count)-th delimiter (from the right).
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

