String Functions
SQL provides a set of built-in string functions for manipulating text data directly within a query - useful for cleaning, formatting, and extracting information from text columns.
Common String Functions
- UPPER() / LOWER() - convert text to uppercase or lowercase
- LENGTH() - returns the number of characters in a string
- TRIM() - removes leading and trailing whitespace
- CONCAT() - joins two or more strings together
- SUBSTRING() - extracts a portion of a string
- REPLACE() - replaces occurrences of a substring with another string
Examples
-- Full name from first and last name
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM customers;
-- Standardize case for comparison
SELECT * FROM customers WHERE LOWER(city) = 'noida';
-- Extract the first 3 characters of a product code
SELECT SUBSTRING(product_code, 1, 3) FROM products;
-- Clean up stray whitespace
SELECT TRIM(first_name) FROM customers;
String function names and exact syntax can vary slightly between database systems (e.g. SUBSTRING vs SUBSTR) - always check your specific database's documentation when in doubt.