The CONCAT() function in SQL is used to combine two or more strings or values into a single string. It can also be used to combine values from multiple columns.
Syntax:
CONCAT(string1, string2, ...)Example
Consider the following employee table:
Query:
SELECT CONCAT(first_name, ' ', last_name) AS full_nameFROM employee;
Output:
- Here, CONCAT() combines the first_name, a space and last_name into a single full_name value.
CONCAT() with NULL Values
CONCAT() treats NULL values as empty strings in SQL Server.
Query:
SELECT CONCAT(first_name, ' ', last_name) AS full_nameFROM employee;
Output:
If last_name is NULL, the result will contain only the first_name and the space.