SQL string functions are powerful tools for manipulating and analyzing text data within databases. These functions allow you to perform various operations on string values, such as concatenation, substring extraction, and case conversion.
The CONCAT function combines two or more strings into a single string.
SELECT CONCAT('Hello', ' ', 'World') AS greeting;
Result: "Hello World"
SUBSTRING extracts a portion of a string based on specified start position and length.
SELECT SUBSTRING('SQL Tutorial', 1, 3) AS extract;
Result: "SQL"
These functions convert strings to uppercase or lowercase, respectively.
SELECT UPPER('hello') AS uppercase, LOWER('WORLD') AS lowercase;
Result: "HELLO" and "world"
LENGTH returns the number of characters in a string.
SELECT LENGTH('SQL Functions') AS string_length;
Result: 13
TRIM removes leading and trailing spaces from a string.
SELECT TRIM(' SQL ') AS trimmed_string;
Result: "SQL"
REPLACE substitutes all occurrences of a substring within a string.
SELECT REPLACE('SQL Tutorial', 'SQL', 'Database') AS new_string;
Result: "Database Tutorial"
CHARINDEX finds the starting position of a substring within a string.
SELECT CHARINDEX('SQL', 'Learn SQL Programming') AS position;
Result: 7
To further enhance your SQL skills, explore these related topics:
Mastering SQL string functions is crucial for effective data manipulation and analysis. These functions provide powerful tools for working with text data in your databases, enabling you to perform complex operations with ease.