Structured Query Language (SQL) is the standard language for managing and manipulating databases. When working with SQL, string functions play a crucial role in manipulating and querying text data stored in databases. These functions enable users to perform tasks such as concatenation, substring extraction, case manipulation, and more. Here, we explore some of the most useful SQL string functions and how they can be utilized effectively.
Also Read: Difference Between MySQL and MS SQL Server

CONCAT()
The CONCAT() function is used to concatenate two or more strings into a single string. It is particularly useful when you need to combine columns or add literal strings together within your SQL queries.
---
Syntax:
1 | CONCAT(string1, string2, ...) |
Example:
1 2 | SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees; |
This query concatenates the first_name and last_name columns from the employees table, separated by a space, and aliases the result as full_name.
SUBSTRING() / SUBSTR()
The SUBSTRING() function extracts a substring from a string, starting at a specified position and optionally for a specified length. Depending on the SQL dialect, it might also be referred to as SUBSTR().
Syntax:
1 | SUBSTRING(string_expression, start_position, length) |
Example:
1 | SELECT SUBSTRING('Hello World', 1, 5) AS result; |
This query extracts the substring ‘Hello’ from the string ‘Hello World’, starting at position 1 and including 5 characters.
UPPER() / LOWER()
The UPPER() and LOWER() functions are used to convert characters in a string to uppercase or lowercase, respectively. These functions are handy when you need to standardize the case of text data for comparison or presentation.
Syntax:
1 2 | UPPER(string_expression) LOWER(string_expression) |
Example:
1 | SELECT UPPER('hello') AS upper_case, LOWER('WORLD') AS lower_case; |
This query converts ‘hello’ to ‘HELLO’ and ‘WORLD’ to ‘world’.
LENGTH() / LEN()
The LENGTH() or LEN() function returns the length (number of characters) of a string. It is useful for various operations where knowing the length of a string is necessary, such as validation or truncation.
Syntax:
1 2 | LENGTH(string_expression) LEN(string_expression) |
Example:
1 | SELECT LENGTH('Hello World') AS string_length; |
This query returns the length of the string ‘Hello World’, which is 11 characters.
TRIM()
The TRIM() function removes leading and trailing spaces (or other specified characters) from a string. This is essential for cleaning up data inputs, especially when dealing with user-generated content or data imports.
Syntax:
1 | TRIM([leading | trailing | both] [trim_character FROM] string_expression) |
Example:
1 | SELECT TRIM(' Hello World ') AS trimmed_string; |
This query trims the leading and trailing spaces from the string ‘ Hello World ‘, resulting in ‘Hello World’.
Also Read: A Comprehensive Guide to Query Optimization in DBMS with MySQL Examples
Conclusion
SQL string functions provide powerful tools for manipulating and querying text data within databases. By understanding and utilizing functions such as CONCAT(), SUBSTRING(), UPPER(), LOWER(), LENGTH(), and TRIM(), SQL users can effectively manage and transform text data to meet their application requirements. These functions not only enhance data integrity and consistency but also improve the efficiency and readability of SQL queries. Mastering these functions equips database professionals with essential skills for data manipulation and retrieval tasks in real-world scenarios.
Also Read: Indispensable MySQL queries for custom fields in WordPress