CONCAT() function in SQL Server
CONCAT() :
This function in SQL Server helps to concatenate two or more strings together. CONCAT() function can accept a minimum of 2 parameters and a maximum of 254 parameters.
Syntax :
CONCAT(string_1, string_2, .......string_n)
Parameters :
- string_1, string_2, …….string_n –
The given strings which need to be concatenated.
Returns :
The function concatenates all the given string and returns them as one whole string.
Applicable to the following versions :
- SQL Server 2017
- SQL Server 2016
- SQL Server 2014
- SQL Server 2012
Example-1 :
The general working of CONCAT() function.
- Concatenating 3 strings together with space in between –
SELECT CONCAT('PYTHON', ' ', 'is', ' ', 'fun!!!') As Combined;
Output :
Combined PYTHON is fun!!! - Concatenating more than 3 strings together –
SELECT CONCAT ('Every', 'next', 'level', 'of', 'your', 'life', 'demands', 'a', 'new', 'you!') As Combined;
Output :
Combined Everynextlevelofyourlifedemandsanewyou!
Example-2 :
Using a variable with CONCAT() function, we assign the strings to variables and then concatenate them.
DECLARE @Str1 AS VARCHAR(100)='Think' DECLARE @Str2 AS VARCHAR(100)='-' DECLARE @Str3 AS VARCHAR(100)='green' DECLARE @Str4 AS VARCHAR(100)=' ' DECLARE @Str5 AS VARCHAR(100)='Be' DECLARE @Str6 AS VARCHAR(100)='-' DECLARE @Str7 AS VARCHAR(100)='green' SELECT CONCAT(@Str1, @Str2, @Str3, @str4, @str5, @str6, @str7) AS Combined;
Output :
Combined |
---|
Think-green Be-green |
Example-3 :
Concatenating numerical expression using CONCAT() function, here in place of string we combined numeric values.
SELECT CONCAT(13, 03, 1999) AS Combined;
Output :
Combined |
---|
1331999 |
Contact Us