SQL CONCAT function is a built-in string function that is used to concatenate two or more strings together. The CONCAT function is available in most of the popular database management systems such as MySQL, Oracle, PostgreSQL, SQL Server, and others.
Syntax
The syntax of the CONCAT function is straightforward. It takes two or more string expressions as its arguments and returns a single string that is the concatenation of all the input strings.
The general syntax of the CONCAT function is:
CONCAT(string1, string2, …, stringN)
Where string1, string2, …, stringN are the strings that you want to concatenate.
Example
For example, to concatenate the first and last name of an employee into a single string, you can use the following SQL query:
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
In this example, the CONCAT function concatenates the first_name and last_name columns of the employees table and adds a space character between them to create a new column called full_name.
You can also use the CONCAT function to concatenate literal strings along with the column values. For example:
SELECT CONCAT('The employee with ID ', id, ' has a salary of $', salary) AS employee_info FROM employees;
In this example, the CONCAT function concatenates three strings: ‘The employee with ID ‘, the id column value, and ‘has a salary of $’, and returns the result in a new column called employee_info.
Overall, the CONCAT function is a useful tool for combining strings in SQL queries, and it can be used in a variety of ways to generate new columns or output messages that include string data.