Microsoft SQL Server Certification Course
- 6k Enrolled Learners
- Weekend
- Live Class
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.
If at all, non-character string values are passed as input. The CONCAT function will implicitly convert those values before the concatenation.
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");
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.
CONCAT Parameters – The only required parameters are the string values that need to be concatenated separated by a comma.
Addition Operator Parameters – It does not require anything more than the strings separated by a comma that needs to be concatenated.
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;
Output: edurekaSQL
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
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.
Course Name | Date | Details |
---|---|---|
Microsoft SQL Server Certification Course | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co