SQL - CONCAT Function



SQL Tutorial - CONCAT Function


The CONCAT function is used to concatenate two specified strings to form a single string.

Concat Syntax


Select firststring + secondstring as CompleteString

The plus sign is used to concatenate two strings. To add space between two strings use space enclosed in single quotes like so(' '). This will be useful in cases wherein you have the first name in one column and the last name in another. To obtain a combination of the first name and last name together in a single column called as full name you need to concatenate the value from a FirstName column and the value of a LastName column to form a name such as John Smith. The ' ' provides the space between John and Smith.

The Concat Example


The Products table has the following records:

ProductID ProductName ProductPrice
Prd001 Pen 40.25
Prd002 Pen 100.75
Prd003 Eraser 10.35
Prd004 Pencil 20.45
Prd005 Book 50.65
Prd006 Gum 25.22

To concatenate the ProductID with ProductNames by using a '-' to separate them, the concat function can be used as shown below.

SELECT (ProductID +' '+'-'+' '+ ProductName ) AS Product FROM Products;

Product
Prd001 - Pen
Prd002 - Pen
Prd003 - Eraser
Prd004 - Pencil
Prd005 - Book
Prd006 - Gum