SQL CONCAT FunctionThe 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-
To understand the CONCAT function in detail, consider an employee_tbl table, which has the following records -
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 -
Example 2:Output:
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. |
id | first_name | last_name | salary | new |
---|---|---|---|---|
1 | Javatpoint | tpoint | 20000 | Java has salary 20000 |
2 | tutorial | &example | 30000 | the tutorial has salary 30000 |
3 | Shane | Watson | 40000 | Shane has salary 40000 |
4 | Jennifer | louse | 60000 | Jennifer has salary 60000 |
Example 2: Using character as well as the number literal
Syntax:
Output:
id | first_name | last_name | salary | new |
---|---|---|---|---|
1 | Javatpoint | tpoint | 20000 | Java100 has id 1 |
2 | tutorial | &example | 30000 | Tutorial100 has id 2 |
3 | Shane | Watson | 40000 | Shane100 has id 3 |
4 | Jennifer | louse | 60000 | Jennifer100 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.