AppDividend
Latest Code Tutorials

SQL CONCAT Function Example | Concat In SQL Tutorial

0

SQL CONCAT function is used to concatenate two strings to form a single string. 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_idFirst_nameLast_nameCityStateSalary
101RohitRajPatnaBihar30000
201ShivamRanaJalandharPunjab20000
301KaranKumarAllahabadUttar Pradesh40000
401SurajBhakatKolkataWest Bengal60000
501AkashCherukuriVizagAndhra Pradesh70000

 

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_idEmp_nameCityStateSalary
101Rohit RajPatnaBihar30000
201Shivam RanaJalandharPunjab20000
301Karan KumarAllahabadUttar Pradesh40000
401Suraj BhakatKolkataWest Bengal60000
501Akash CherukuriVizagAndhra Pradesh70000

 

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
  2. SQL Insert Query Tutorial
  3. SQL Where Clause Example
  4. SQL SELECT Query Example
  5. SQL Update Query Example

Leave A Reply

Your email address will not be published.

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