Learn About Concatenate In SQL With Examples

Last updated on Mar 18,2024 15.3K Views

Learn About Concatenate In SQL With Examples

edureka.co

Concatenation, in general, refers to binding a bunch of strings into a single string. In SQL, this is achieved by a function named CONCAT(). It takes up to 255 input strings and joins them together. In this article, we will learn how we can use the CONCAT() function in SQL. The following topics are covered in this blog:

CONCAT Function In SQL

In SQL, the concatenation of strings is achieved by the CONCAT() function. There are a few things you should keep in mind while using the CONCAT function.

How To Use CONCAT In SQL

To understand how we can use CONCAT in SQL, let us take a simple example. So ideally concatenation works like- let’s say we have two strings, “edureka”, “SQL”. And if we concatenate these two strings, we will get a resultant string or concatenated string as “edureka SQL”. It works the same with the CONCAT function as well.

Let’s say we have the same strings “edureka” and “SQL”, to concatenate these two strings we will write the following command.

SELECT CONCAT("edureka", "SQL");

Output: edurekaSQL

We can use the addition “+” operator to add two or more strings together.

SELECT "edureka" + "SQL";

Output:edurekaSQL

To separate the strings with a separator, we can use CONCAT_WS() function as well. Take a look at an example below to understand how it works.

SELECT CONCAT_WS("-" , "EDUREKA", "SQL");

Output: EDUREKA-SQL

So you can use either of these approaches to concatenate strings in SQL. Let us take one more look at the parameters that we pass into the CONCAT function.

Concatenation Parameters

CONCAT Function Examples

Let us take a simple example using the string literals.

SELECT 'edureka' + 'SQL' as full_name;

Output: edurekaSQL

Let us take one more example

SELECT CONCAT('edureka', 'sql');

Output: edurekasql

Now let us try to understand how concatenation works with table values.

Using CONCAT With Table Values

Let us consider a table with the following values.

Now let us try to concatenate the first name and the last name.

SELECT first_name,last_name, 
CONCAT(first_name,' ',last_name)full_name 
FROM N
ORDER BY full_name

Output:

Considering the null values in a table, let us understand how concatenation works with null values.

Using CONCAT With Null Values

Let us suppose we have a few null values in the table. When the value is NULL the CONCAT function uses empty for concatenation.

SELECT first_name,last_name,phone, 
CONCAT(first_name,' ',last_name,phone)full_name 
FROM N
ORDER BY full_name

Output:

So that was all about Concatenation in SQL, I hope this article has helped you in adding value to your knowledge. For more information on SQL or Databases, you can refer to our comprehensive reading list here: Databases Edureka.

If you wish to get structured training on MySQL or MS SQL, then check out our MySQL DBA Certification Training  and  SQL Course which comes with instructor-led live training and real-life project experience. This training will help you understand MySQL in-depth and help you achieve mastery over the subject.

Got a question for us? Please mention it in the comments section of ”Concatenate SQL” and I will get back to you.

Upcoming Batches For Microsoft SQL Server Certification Course
Course NameDateDetails
Microsoft SQL Server Certification Course

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Analyzing Customer-Product Relationships for Business Growth with Tableau