intro
Master the SQL CONCAT function with this comprehensive guide to SQL string concatenation techniques.
When working with databases, combining text values is a common requirement for data manipulation, reporting, and presentation. The SQL CONCAT
function provides a powerful way to merge multiple strings into a single output, enabling everything from simple name formatting to complex dynamic query construction.
This essential database operation allows you to combine multiple text elements into unified data points—a process performed routinely in database management. With how frequently developers need to perform this operation, database systems include dedicated functions like CONCAT
to streamline this process efficiently.
In this comprehensive guide, you will learn how to effectively use CONCAT
in SQL queries with practical examples.
What Is CONCAT in SQL?
In SQL, CONCAT
is a string function that combines two or more strings into a single string. While the basic functionality remains consistent across different DBMS technologies, the syntax and behavior can vary slightly.
In detail, this versatile function allows you to join text from multiple columns and combine fixed text with database values.
Let's look at how you can leverage this SQL operation across various scenarios.
CONCAT in SQL Queries: Syntax Across Major Database Systems
Different database platforms implement concatenation with slight variations:
Common Databases:
The following syntax applies to MySQL, PostgreSQL, and SQL Server:
1
CONCAT(string1, string2, ..., stringN)
Oracle:
1
-- Oracle accepts only two arguments
2
CONCAT(string1, string2)
3
4
-- For multiple strings in Oracle, use:
5
CONCAT(CONCAT(string1, string2), string3)
In most database systems, string1, string2, ..., stringN
represents a list of string values which are separated by commas.
Note: The CONCAT
function must be called with at least one argument (except in Oracle where exactly two arguments are required). Otherwise, your database engine will throw this syntax error:
1
Incorrect parameter count in the call to native function 'CONCAT'
First Example
The CONCAT
function produces a combined string by joining all provided arguments together. To illustrate this functionality, let’s look at the following query example:
1
-- Works in: MySQL, PostgreSQL, SQL Server
2
SELECT CONCAT('Okay', ', ', 'Awesome!');
3
4
-- Oracle version (using || operator for multiple strings)
5
SELECT 'Okay' || ', ' || 'Awesome!' FROM dual;
You will have:
1
"Okay, Awesome!"

Notes:
When using string concatenation functions, you must keep in mind these DBMS-specific behaviors:
PostgreSQL:
MySQL:
SQL Server:
Oracle:
Time to look at some examples!
Basic Examples of CONCAT in SQL Queries
Let's start with some simple examples.
Note: All queries below are going to be executed in DbVisualizer—the database client with the highest user satisfaction in the world. Any other database client will do.
Example #1: Basic String Joining
Use the standard CONCAT
function for common scenarios as below:
1
SELECT CONCAT('Hello', ' ', 'World') AS greeting;

Example #2: Combining Column Values
Imagine we have a customers table with separate first_name
and last_name
columns:
1
CREATE TABLE reporters (
2
reporter_id INT PRIMARY KEY,
3
first_name VARCHAR(50),
4
last_name VARCHAR(50),
5
email VARCHAR(100)
6
);
7
8
INSERT INTO customers VALUES
9
(1, 'Timo', 'Cruz', 'teecruz001@gmail.com'),
10
(2, 'Junior', 'Butler', 'j.butler_1@gmail.com'),
11
(3, 'Kenyon', 'Guard', 'k.guard@gmail.com');
To create full names, we are going to concatenate these columns as shown:
1
SELECT CONCAT(first_name, ' ', last_name) AS full_name
2
FROM reporters;

Example #3: Creating Formatted Outputs
1
SELECT
2
CONCAT(first_name, ' ', last_name, ' <', email, '>') AS formatted_contact
3
FROM
4
reporters;

Handling NULL Values with CONCAT
One important consideration when using CONCAT
is how it handles NULL values. Different database systems handle NULL values in string concatenation differently:
CONCAT in SQL Queries: Use Cases
Time to look at some of the most common CONCAT
string joining cases.
Column Concatenation
Using CONCAT
to combine values from several columns into a single output is its most popular use case.
Assume, for instance, that you have a table called reporters
with the columns first_name
and last_name
and you want to combine both names into a complete name, you are going to approach it this way:
1
SELECT
2
first_name,
3
last_name,
4
CONCAT(first_name, ' ', last_name) AS full_name
5
FROM
6
reporters;

Great. It can be seen that the full_name
column in the output shown in DbVisualizer above is the concatenation of first_name
and last_name
as expected.
URL Construction using CONCAT
Another importance of the CONCAT
function is particularly in the use of the construction of dynamic URLs from database values.
A dynamic URL is simply a URL of a web page dynamically generated from the database records.
To practice how this works, copy the following SQL code and execute it in the SQL commander in DbVisualizer:
1
-- Create the products table with necessary columns
2
-- This syntax works across most major DBMS
3
CREATE TABLE products (
4
product_id INT PRIMARY KEY,
5
product_name VARCHAR(100) NOT NULL,
6
category VARCHAR(50) NOT NULL,
7
slug VARCHAR(100) NOT NULL,
8
price DECIMAL(10,2),
9
stock INT
10
);
11
12
-- Insert sample product data
13
INSERT INTO products (product_id, product_name, category, slug, price, stock) VALUES
14
(101, 'Ergonomic Chair', 'furniture', 'ergonomic-chair', 299.99, 45),
15
(102, 'Wireless Mouse', 'electronics', 'wireless-mouse', 49.99, 120),
16
(103, 'Coffee Maker', 'appliances', 'coffee-maker', 89.99, 35);
17
18
-- URL construction query - MySQL, PostgreSQL, SQL Server version
19
SELECT
20
product_id,
21
product_name,
22
category,
23
CONCAT('https://example.com/products/', category, '/', product_id, '-', slug) AS product_url
24
FROM
25
products;
Let’s see the output in DbVisualizer:

Amazing! This technique allows developers to generate SEO-friendly URLs directly from database queries. The aim? To ensure there is consistency between database records and web addresses.
Conclusion
Throughout this comprehensive guide, we've learned the capabilities of SQL's CONCAT
function—a fundamental yet sophisticated tool that transforms how data professionals manipulate and present information. From basic string joining to complex conditional formatting, CONCAT
serves as the bridge between raw database values and meaningful human-readable content.
To truly experience the power of SQL string concatenation, a sophisticated visual database tool is invaluable. DbVisualizer stands out as the premier solution, providing the right tool that accelerates development through its drag-and-drop query building, advanced query optimization capabilities, and in-line data editing functionality. With comprehensive support for PostgreSQL, MySQL, Oracle, and virtually any other database system, DbVisualizer empowers you to implement these CONCAT
techniques effortlessly across your entire data ecosystem.
Download DbVisualizer for free and experience firsthand how the right combination of SQL knowledge and professional tools can transform your approach to data management and presentation.
FAQ
What is the CONCAT function in SQL?
The CONCAT
function in SQL combines two or more strings into a single string, allowing you to join text values from different columns or literal values.
How do I add a space between values when using SQL CONCAT?
To add a space between values in SQL CONCAT, include a space character as a separate argument:
1
CONCAT(first_name, ' ', last_name)
What's the difference between CONCAT and CONCAT_WS in SQL?
CONCAT
combines strings with separately specified separators, while CONCAT_WS
(With Separator) applies a single separator between all values:
1
-- With CONCAT, you need to specify each separator
2
CONCAT(first_name, ' ', middle_name, ' ', last_name)
3
4
-- With CONCAT_WS, you specify the separator once (available in MySQL and PostgreSQL)
5
CONCAT_WS(' ', first_name, middle_name, last_name)
Is there a limit to how many strings I can join with SQL CONCAT?
Most database systems allow numerous arguments in CONCAT
:
What are the performance implications of using CONCAT in SQL queries?
While CONCAT
operations are generally efficient, there are some performance considerations: