Concat Function in SQL
? CONCAT Function in SQL
The CONCAT() function is used to concatenate (join) two or more strings into one single string.
? Syntax
CONCAT(string1, string2, ..., stringN)Takes two or more string arguments.
Returns a single string that is the combination of all input strings.
If any argument is
NULL, in most databases, it treats it as an empty string (behavior may vary).
? Examples
1. Concatenate two strings
SELECT CONCAT('Hello', ' ', 'World') AS greeting;-- Result: 'Hello World'2. Concatenate columns
Suppose you have a table employees with columns first_name and last_name:
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;3. Handling NULL values (in MySQL)
SELECT CONCAT('Name: ', NULL, ' Smith') AS result;-- Result: 'Name: Smith' (NULL treated as empty string)? Notes
In some databases (like SQL Server), use the
+operator orCONCAT()(from SQL Server 2012+).For Oracle, use
||operator for string concatenation:
SELECT first_name || ' ' || last_name AS full_name FROM employees;If you want, I can provide examples for a specific database or show alternatives for concatenation!