SQL CONCAT Function

The CONCAT function in SQL is a String function, which is used to merge two or more strings. The Concat service converts the Null values to an Empty string when we display the result. This function is used to concatenate two strings to make a single string. The operator is used to link character strings and column string.

We can use a literal in CONCAT Function. A literal is a number, character, or date that includes the SELECT statement.

Syntax of CONCAT function:

Example-


CONCAT(' FIRST','SECOND')FIRST SECOND

To understand the CONCAT function in detail, consider an employee_tbl table, which has the following records -


IDNAMEWORK_DATEDAILY_TYPING_PAGES
1Michal2009-02-15270
2Zeena2003-03-24250
2kachner2007-08-19277
2warner2007-04-25264
3Joy2007-05-17250
4atire2006-06-23270
5delph2004-05-28230

So if we want to concatenate all the names, employee IDs, and work_ date of above table, then we can do it using the following command -

1Michal2009-02-15
2Zeena2003-03-24
2kachner2007-08-19
2warner2007-04-25
3joy2007-05-17
4atire2006-06-23
5delph2004-05-28

Example 2:

Output:

idlast_namefirst_namefirst_name||last_namesalaryfirst_name||salary
1beanMr.Mr.bean10000Mr.10000
2WilliamSunitaSunita William50000Sunita50000
3tpointJavaJavatpoint20000Java20000
4&exampletutorialtutorial&example90000Tutorial90000

Note: In above example, we have used "||", which is known as the Concatenation operator, and it is used to link two or more columns in select query. This operator is independent of the data type of column.
Here, we have linkined 2 columns i.e, first_name+last_name as well as first_name+salary.

We can use string literals in CONCAT operator.

Example 1: Using the character literal

Syntax

Output: (Concatenating three values and giving a new 'name')

idfirst_namelast_namesalarynew
1Javatpointtpoint20000Java has salary 20000
2tutorial&example30000the tutorial has salary 30000
3ShaneWatson40000Shane has salary 40000
4Jenniferlouse60000Jennifer has salary 60000

Note: We have used salary as a character literal in the select statement. We can use the date literal and number literal according to our requirement in the table.

Example 2: Using character as well as the number literal

Syntax:

Output:

idfirst_namelast_namesalarynew
1Javatpointtpoint20000Java100 has id 1
2tutorial&example30000Tutorial100 has id 2
3ShaneWatson40000Shane100 has id 3
4Jenniferlouse60000Jennifer100 has id 4

In the above example, we have used the salary as a character literal as well as 100 as number authentic in our select statement.


Next TopicSQL Tutorial




Latest Courses