MariaDB String Functions Guide
String Functions Guide
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.SQLSELECT UCASE(col1) AS Upper_Col1, LCASE(col2) AS Lower_Col2 FROM table4;
Padding Strings:
LPAD(str, len, padstr): Left-padsstrwithpadstruntil it islencharacters long.RPAD(str, len, padstr): Right-padsstrwithpadstruntil it islencharacters long.SQLSELECT RPAD(part_nbr, 8, '.') AS 'Part Nbr.', LPAD(description, 15, '_') AS Description FROM catalog;Example:
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.SELECT TRIM(LEADING '.' FROM col1) AS Trimmed_Leading_Dots, TRIM(TRAILING FROM col2) AS Trimmed_Trailing_Spaces, -- Trims spaces TRIM(BOTH '_' FROM col3) AS Trimmed_Both_Underscores, TRIM(col4) AS Trimmed_Spaces -- Trims leading and trailing spaces FROM table5;
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.SELECT LEFT(telephone, 3) AS area_code, RIGHT(telephone, 7) AS tel_nbr FROM contacts ORDER BY area_code;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.SELECT CONCAT('(', LEFT(telephone, 3), ') ', SUBSTRING(telephone, 4, 3), '-', MID(telephone, 7)) AS 'Telephone Number' FROM contacts ORDER BY LEFT(telephone, 3);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.SELECT CONCAT(REPLACE(title, 'Mrs.', 'Ms.'), ' ', name_first, ' ', name_last) AS Name FROM contacts;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.-- Example: Change 'Mrs.' to 'Ms.' where title is embedded in a 'name' column SELECT INSERT(name, LOCATE('Mrs.', name), LENGTH('Mrs.'), 'Ms.') FROM contacts WHERE name LIKE '%Mrs.%';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.SELECT REVERSE('MariaDB'); -- Output: BDeiraMREPEAT(str, count): Repeatsstrcounttimes.SELECT REPEAT('Ha', 3); -- Output: HaHaHa
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.SELECT COUNT(school_id) AS 'Number of Students' FROM table8 WHERE CHAR_LENGTH(school_id) = 8;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:
SELECT ip_address FROM computers WHERE server = 'Y' ORDER BY INET_ATON(ip_address) LIMIT 3;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.
SELECT col1, col2 FROM table6 WHERE STRCMP(col3, 'text') = 0; -- Finds exact case-sensitive match for 'text'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).
-- Get the first two elements from a pipe-delimited string SELECT SUBSTRING_INDEX('elem1|elem2|elem3|elem4', '|', 2); -- Output: elem1|elem2 -- Get the last two elements SELECT SUBSTRING_INDEX('elem1|elem2|elem3|elem4', '|', -2); -- Output: elem3|elem4
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

