Concatenate in SQL
? Concatenate Strings in SQL
Concatenation means joining two or more strings together to form a single string.
? Common Ways to Concatenate in Different SQL Databases
| Database | Method / Function | Example |
|---|---|---|
| MySQL | CONCAT() function | SELECT CONCAT(first_name, ' ', last_name) FROM employees; |
| PostgreSQL | ` | |
| SQL Server | + operator or CONCAT() (SQL Server 2012+) | SELECT first_name + ' ' + last_name FROM employees; or SELECT CONCAT(first_name, ' ', last_name); |
| Oracle | ` |
? Examples
Using CONCAT() (MySQL, PostgreSQL, SQL Server 2012+)
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;Using || Operator (PostgreSQL, Oracle)
SELECT first_name || ' ' || last_name AS full_name FROM employees;Using + Operator (SQL Server)
SELECT first_name + ' ' + last_name AS full_name FROM employees;? Notes
CONCAT()can take multiple arguments.When concatenating
NULLwith a string:CONCAT()treatsNULLas an empty string.Using
||or+operator may returnNULLif any operand isNULL(depends on DBMS).
Use functions like
COALESCE()to handleNULLvalues when concatenating.
If you want examples specific to a database system or handling NULLs in concatenation, just ask!