CONCAT SQL | SQL Server CONCAT() Function With Example

Summary: in this tutorial, you will see What is CONTACT SQL and How to use the SQL Server CONCAT() function to join multiple strings into one string.

Definition and Usage

The CONCAT SQL Server function used to adds two or more strings together or To join two or more strings into one, there is the following syntax

CONTACT SQL Synatx

CONCAT ( input_string1, input_string2 [, input_stringN ] );

The SQL CONCAT() takes two up to 255 input strings and joins them into one. Therefore, it requires at least two input strings. If you pass one input string, the CONCAT() function will raise an error.

Furthermore, if you pass the non-character string values, then the SQL CONCAT() function will implicitly convert those values into strings before concatenating.

The CONCAT in SQL, a function also converts NULL into an empty string with the type VARCHAR(1).

.
Note: To add a separator during the concatenation, then you will use the CONCAT_WS() function.

CONCAT SQL Function Examples

Let’s see the some example to get familiar with the SQL CONCAT() function.

A) Using SQL CONCAT() function with literal strings

The following given below example uses the CONCAT SQL function to concatenate three literal string David, space, and Shayala:

SELECT 
    'David' + ' ' + 'Shayala' AS full_name;

Therefore, let’s execute the query to return the output.

full_name
---------
David Shayala
(1 row affected)
B) Using CONCAT() function with table columns

Here, we will use the Sample Demo table "Customers".

CustomerName ContactName Address City PostalCode Country
Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 5021 Mexico
Blauer See Delikatessen Hanna Moos Forsterstr. 57 Mannheim 68306 Germany
Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 5023 Mexico
Blondel père et fils Frédérique Citeaux 24, place Kléber Strasbourg 67000 France
Bs Beverages Victoria Ashworth Fauntleroy Circus London 87000 UK
Bottom-Dollar Marketse Elizabeth Lincoln 23 Tsawassen Blvd. Tsawassen 45300 Canada
Bon app Laurence Lebihans 12, rue des Bouchers Marseille 13008 France
Bólido Comidas preparadas Martín Sommer C/ Araquil, 67 Madrid 28023 Spain
Consolidated Holdings Elizabeth Brown Berkeley Gardens 12 Brewery London 98760 UK
Comércio Mineiro Pedro Afonso Av. dos Lusíadas, 23 São Paulo 5432 Brazil
Chop-suey Chinese Yang Wang Hauptstr. 29 Bern 3012 Switzerland
Centro comercial Moctezuma Francisco Chang Sierras de Granada 9993 México D.F. 5022 Mexico
Du monde entier Janine Labrune 67, rue des Cinquante Otages Nantes 44000 France
Drachenblut Delikatessend Sven Ottlieb Walserweg 21 Aachen 52066 Germany
Ernst Handel Roland Mendel Kirchgasse 6 Graz 8010 Austria
Eastern Connection Ann Devon 35 King George London 11036 UK
Franchi S.p.A. Paolo Accorti Via Monte Bianco 34 Torino 10100 Italy
Furia Bacalhau e Frutos do Mar Lino Rodriguez Jardim das rosas n. 32 Lisboa 1675 Portugal
France restauration Carine Schmitt 54, rue Royale Nantes 44000 France
Frankenversand Peter Franken Berliner Platz 43 München 80805 Germany
Folk och fä HB Maria Larsson Åkergatan 24 Bräcke 885634 Sweden
Folies gourmandes Martine Rancé 184, chaussée de Tournai Lille 59000 France
FISSA Fabrica Inter. Salchichas S.A. Diego Roel C/ Moralzarzal, 86 Madrid 28034 Spain

The following statement uses the SQL CONCAT() function to concatenate values in the CustomerName, City, Country, and PostalCode columns of the customers table:

SELECT 
    CustomerName,
    City,
	Country,
	PostalCode,
    CONCAT(CustomerName, ' ', City, '', Country, '', PostalCode) full_address
FROM 
    customers
ORDER BY 
    CustomerName;

The following given below picture shows the output:

CONCAT SQL Function

C) Using CONCAT() function with NULL

Here, we will use the Sample Demo table "Customers".

CustomerName ContactName Address City PostalCode Country
Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209
Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 5021 Mexico
Blauer See Delikatessen Hanna Moos Forsterstr. 57 Mannheim 68306
Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 5023 Mexico
Blondel père et fils Frédérique Citeaux 24, place Kléber Strasbourg 67000 France
Bs Beverages Victoria Ashworth Fauntleroy Circus London 87000 UK
Bottom-Dollar Marketse Elizabeth Lincoln 23 Tsawassen Blvd. Tsawassen 45300 Canada
Bon app Laurence Lebihans 12, rue des Bouchers Marseille 13008 France

This following given below example uses the SQL CONCAT() function to format addresses for customers:

SELECT 
    CONCAT(
        CHAR(12),
        CustomerName,
        CHAR(12),
        City,
        CHAR(12),
        CONCAT(PostalCode,' ',Country)
    ) customer_address
FROM
    customers
ORDER BY 
    CustomerName

The following given below picture shows the output:

concat function output
Therefore, as you can see in the output image if the customers have not to mention the country name, then the CONCAT() function used an empty for the concatenation.

.
Note: we used the SQL CHAR() function to get the new line character in this example.

Conclusion

In this tutorial, you have learned What is SQL CONCAT() Function and How to use the SQL Server CONCAT() function to join two or more strings into one.