String Concatenation Operator (Database Engine)

The string concatenation operator is the plus sign (+). You can combine, or concatenate, two or more character strings into a single character string. You can also concatenate binary strings. The following is an example of concatenation:

SELECT ('abc' + 'def');

Here is the result set.

------
abcdef

(1 row(s) affected)

The following query displays names of the first four contacts under the Moniker column in last name, first name order, with a comma and space after the last name.

USE AdventureWorks;
GO
SELECT LastName + ', ' + FirstName AS Moniker 
FROM Person.Contact
WHERE ContactID < 5;

Here is the result set.

Moniker
-------------------------
Achong, Gustavo
Abel, Catherine
Abercrombie, Kim
Acevedo, Humberto

(4 row(s) affected)

Other data types, such as datetime and smalldatetime, must be converted to character strings by using the CAST conversion function before they can be concatenated with a string.

USE AdventureWorks;
GO
SELECT 'The due date is ' + CAST(DueDate AS varchar(128))
FROM Sales.SalesOrderHeader
WHERE SalesOrderID = 43659;
GO

Here is the result set.

---------------------------------------
The due date is Jul 13 2001 12:00AM

(1 row(s) affected)

The empty string ('') is evaluated as a single space:

SELECT 'abc' + '' + 'def'

Here is the result set.

-------
abcdef

(1 row(s) affected)

Note

Whether an empty string ('') is interpreted as a single blank character or as an empty character is determined by the compatibility level setting of sp_dbcmptlevel. For this example, if sp_dbcmptlevel is 65, empty literals are treated as a single blank.

When the input strings both have the same collation, the output string has the same collation as the inputs. When the input strings have different collations, the rules of collation precedence determine the collation of the output string. You can also assign a specific collation by using the COLLATE clause.

See Also

Other Resources

COLLATE (Transact-SQL)
Collation Precedence (Transact-SQL)
Operators (Transact-SQL)
sp_dbcmptlevel (Transact-SQL)
+ (String Concatenation) (Transact-SQL)

Help and Information

Getting SQL Server 2005 Assistance