MS SQL Server CONCAT() Function

Sometimes you may need to concatenate or join multiple strings in MS SQL Server. There is a function called CONCAT in MS SQL Server by which you can do this. CONCAT function returns a string resulting from the concatenation, or joining, of two or more string values in an end-to-end manner.

Let’s see the function syntax:

CONCAT(string_value1, string_value2 [,string_valueN]) ;

In this syntax:

  • The CONCAT function requires at least two string_value arguments, and no more than 254 string_value arguments.
  • Parameter string_value to concatenate to the other values. .
  • Function returns nvarchar if one of the input arguments is of the nvarchar data type; otherwise,  returns varchar.   If CONCAT receives arguments with all NULL values, it will return an empty string of type varchar(1). Return type of varchar can be at most 8000 characters and return type of  nvarchar,  can be at most 4,000 characters.
  • Finally function returns concatenated string.

The following example uses CONCAT function to join the strings.

DECLARE @v1 varchar(30) = 'MS';
DECLARE @v2 varchar(30) = 'SQL Server';
DECLARE @v3 varchar(30) = 'is a';
DECLARE @v4 varchar(30) = 'RDBMS';
SELECT CONCAT(@v1,' ',@v2,' ',@v3,' ',@v4) result;

Here is the result set.

result
MS SQL Server is a RDBMS

I have an another post MS SQL Server String Related Functions where you will learn about different string related functions of MS SQL Server.

In this tutorial, I have shown you how to join multiple string using CONCAT function in MS SQL Server. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page http://www.facebook.com/freetechtrainer and stay connected.

Add a Comment