CONCAT Function in SQL

This string function adds two or more characters or strings to form a single new string in the result. If you pass only one string in the function, then it shown an error in the output. So, CONCAT() function requires at least two strings.

Syntax of CONCAT String Function

Syntax1: This syntax uses the CONCAT function with two or more column names of the SQL table:

In the syntax, we have to specify the name of those columns whose values we want to concatenate.

Syntax2: This syntax uses the CONCAT function with two or more strings:

Syntax3: This syntax uses the CONCAT function with two or more characters:

Examples of CONCAT String function

Example 1: The following SELECT query adds two characters to form a new string:

Output:

'Hi'    

Example 2: The following SELECT query gives a new string by adding more than 2 characters:

Output:

Wishes
Congratulations

Example 3: The following SELECT query adds two strings:

Output:

Sentence
NEW DELHI IS THE CAPITAL OF INDIA

Example 4: The following SELECT query adds more than two strings:

Output:

Have a Nice Day   

Example 5: The following SELECT query adds those two string which contain symbols:

Output:

#@##9Ski Java958@%^&   

Example 6: The following SELECT query use space between two strings:

Output:

JavaTpoint Website.  

Example 7: This example uses the CONCAT function with the table in Structured Query Language.

In this example, we have to create a new SQL table through which we will execute the Concat() function on columns. The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Student_Marks table:

The below INSERT queries insert the records of college Faculties in the Student_Marks table:

The following SELECT statement displays the inserted records of the above Student_Marks table:


Student_IdStudent_First_NameStudent_Middle_NameStudent_Last_NameStudent_ClassStudent_CityStudent_StateStudent_Marks
4001AmanRoySharma4ChandigarhPunjab88
4002VishalGurrSharma8MurthalHaryana95
4007RajsinghaniaGupta6GhaziabadUttar Pradesh91
4004YashChopraSinghania9JaipurRajasthan85
4011VinaySharmaRoy8ChandigarhPunjab94
4006ManojSinghaniaGupta5GhaziabadUttar Pradesh83
4010RamRaheemGupta9LucknowUttar Pradesh89

Query 1: The following SELECT query uses the CONCAT function with the Student_First_Name, Student_Middle_Name, and Student_Last_Name columns of the above Student_Marks table:

This SQL statement concatenates the values of the first name, middle name, and last name of each student.

Output:

Student_First_NameStudent_Middle_NameStudent_Last_NameName
AmanRoySharmaAmanRoySharma
VishalGurrSharmaVishalGurrSharma
RajsinghaniaGuptaRajsinghniaGupta
YashChopraSinghaniaYashChopraSinghania
VinaySharmaRoyVinaysharmaRoy
ManojSinghaniaGuptaManojSinghaniaGupta
RamRaheemGuptaRamRaheemGupta

Query 2: The following SELECT query uses the CONCAT function with the Student_City and Student_State column of those Student whose Student_Id is greater than 4002 in the above Student_Marks table:

This SQL statement adds the values of city and state with a comma.

Output:

Student_IdAddress
4007Ghaziabad, Uttar Pradesh
4004Jaipur, Rajasthan
4011Chandigarh, Punjab
4006Ghaziabad, Uttar Pradesh
4010Lucknow, Uttar Pradesh





Latest Courses