Dofactory.com
Dofactory.com
Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

SQL CONCAT Function

The CONCAT function joins two or more string values together.

CONCAT converts all arguments to string types before concatenation.

CONCAT converts any NULL argument to an empty string.

Example

#

This example joins three string values into a single string.

SELECT CONCAT('Lars',' ','Anderson') AS Name
Result:  1 record
Name
Lars Anderson

Using CONCAT

#

CONCAT takes two or more arguments.

The return value is either VARCHAR or NVARCHAR depending on the input types.

Syntax

Syntax for the CONCAT function.

CONCAT (value1, value2, ...., valueN)

values -- required. Two or more parameters that can be converted to string.


More Examples

CONCAT. String values.

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: List customers with first and last names combined.
SELECT CONCAT(FirstName, ' ', LastName) AS Name, 
       City, Country
  FROM Customer
Result:  91 records
Name City Country
Maria Anders Berlin Germany
Ana Trujillo México D.F. Mexico
Antonio Moreno México D.F. Mexico
Thomas Hardy London UK
Christina Berglund Luleå Sweden

CONCAT. Numeric values.

Problem: List the price calculations for order #542382.
SELECT OrderNumber, ProductName, 
       CONCAT(I.Quantity, ' x $', NULL,
              I.UnitPrice, ' = $', 
              I.UnitPrice * I.Quantity) AS Calculation
  FROM OrderItem I
  JOIN [Order] O ON O.Id = I.OrderId
  JOIN Customer C ON C.Id = O.CustomerId
  JOIN Product P ON P.Id = I.ProductId
 WHERE OrderNumber = '542382'
Result:  3 records
OrderNumber ProductName Calculation
542382 Sir Rodney's Marmalade 40 x $64.80 = $2592.00
542382 Geitost 25 x $2.00 = $50.00
542382 Camembert Pierrot 40 x $27.20 = $1088.00

You may also like



Last updated on Dec 21, 2023

Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.