AppDividend
Latest Code Tutorials

SQL CONCAT Function Example | Concat In SQL Tutorial

0

SQL CONCAT Function Example | Concat In SQL Tutorial is today’s topic. SQL CONCAT function is used to concatenate two strings to form a single string. The SQL CONCAT function is used to join two or more strings. It takes up to 255 input strings which are further joined by the function. For, performing join operation CONCAT requires at least 2 strings. If it is provided with only one string, it will raise an error.

SQL CONCAT Function

If any non-character string values are passed in the function, it will be implicitly converted to strings before concatenating. If any NULL is added to the function, it converts it into an empty string with VARCHAR (1).

Syntax

SELECT CONCAT (string1, string2, ... string_n)

Parameters

CONCAT: This is the function that is used for concatenating.

string1, string2, … string_n: These are the strings that are used for concatenating.

See the following query.

Select concat (‘Appdividend’, ‘.com’);

See the output.

Appdividend.com

See the query.

Select concat (‘App’, ‘Div’, ‘idend’, ‘.com’);

See the output.

Appdividend.com

See the following query.

Select concat ('Love', ' ', 'is', ' ', 'Life');

See the output.

Love is Life

Here we used space as another character to concatenate with the words. Adding space prevents our values from being squished together.

See the following query.

SELECT CONCAT ('Let', '''', 's learn MySQL');

See the output.

Let's learn MySQL

In the above example, we have used the second parameter within the CONCAT function to add a single quote into the middle of the resulting string.

Above were the common examples to make clear how concat function works.

Let’s see the example with proper tables.

Consider table Employee

Emp_id First_name Last_name City State Salary
101 Rohit Raj Patna Bihar 30000
201 Shivam Rana Jalandhar Punjab 20000
301 Karan Kumar Allahabad Uttar Pradesh 40000
401 Suraj Bhakat Kolkata West Bengal 60000
501 Akash Cherukuri Vizag Andhra Pradesh 70000

 

Now suppose we want to concatenate first_name and last_name under the single column name Emp_name then the following query has to be used.

See the following query.

Select Concat (First_name, ' ', Last_name) AS Emp_name 
from Employee;

See the output.

Emp_id Emp_name City State Salary
101 Rohit Raj Patna Bihar 30000
201 Shivam Rana Jalandhar Punjab 20000
301 Karan Kumar Allahabad Uttar Pradesh 40000
401 Suraj Bhakat Kolkata West Bengal 60000
501 Akash Cherukuri Vizag Andhra Pradesh 70000

 

Here you can see that names were concatenated under a single column name Emp_name. Here we have also used Space as an additional character to add space between two names to avoid confusion between the name.

Finally, SQL CONCAT Function Example | Concat In SQL Tutorial is over.

#Recommended Posts

  1. SQL Create Table Statement Example | Create Table in SQL Tutorial
  2. SQL Insert Query Tutorial | SQL Insert Into Statement Example
  3. SQL Where Clause Example | SQL Where Query Tutorial
  4. SQL SELECT Query Example | SQL SELECT Statement Tutorial
  5. SQL Update Query Example | SQL Update Statement Tutorial

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.