SQL String Functions

SQL String functions are a set of built-in functions that are used to manipulate strings or character data in SQL. These functions can be used to extract, transform and manipulate data stored in character data type columns.

Here are some of the most commonly used SQL string functions:

CONCAT This function is used to concatenate two or more strings into a single string.

SELECT CONCAT('Hello', 'World');
Output: HelloWorld

CONCAT_WS It is used to concatenate multiple strings or expressions into a single string, separating each element with a specified separator. The syntax typically involves providing the separator as the first argument, followed by the strings or expressions to be concatenated. Unlike the regular CONCAT function, CONCAT_WS allows you to avoid explicitly handling separator placement between elements, making it particularly useful when dealing with lists or combining values with a consistent separator.

SELECT CONCAT_WS(' ', 'Hello', 'World');
Output: Hello World

SUBSTRING This function is used to extract a substring from a given string.

SELECT SUBSTRING('Hello World', 1, 5);
Output: Hello

LEN This function is used to get the length of a string.

SELECT LENGTH('Hello World');
Output: 11

REPLACE This function is used to replace a part of a string with another string.

SELECT REPLACE('Hello World', 'Hello', 'Hi');
Output: Hi World

UPPER This function is used to convert a string to uppercase.

SELECT UPPER('Hello World');
Output: HELLO WORLD

LOWER This function is used to convert a string to lowercase.

SELECT LOWER('Hello World');
Output: hello world

TRIM This function is used to remove extra spaces from the beginning and end of a string.

SELECT TRIM(' Hello World ');
Output: Hello World

These are just a few of the many SQL string functions that can be used to manipulate strings in a database. Knowing these functions can help you to efficiently query and manipulate data in your database.