SQL Essentials Training & Certification
- 8k Enrolled Learners
- Self Paced
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:
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.
The CONCAT function raises an error if only one string is passed as input. There has to be at least two strings as input for the CONCAT function to work without any hassles.
The CONCAT function can take up to 255 input strings for concatenation.
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");
We can use the addition “+” operator to add two or more strings together.
SELECT "edureka" + "SQL";
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");
CONCAT Parameters – The only required parameters are the string values that need to be concatenated separated by a comma.
CONCAT_WS Parameters – The first parameter is the separator that you want to use, after that all the strings that are being concatenated are added, all separated by a comma.
Let us take a simple example using the string literals.
SELECT 'edureka' + 'SQL' as full_name;
Let us take one more example
SELECT CONCAT('edureka', 'sql');
Now let us try to understand how concatenation works 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
Considering the null values in a table, let us understand how concatenation works 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
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 a structured training on MySQL, then check out our MySQL DBA Certification Training 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.