Concatenate string and Int in SQL server

In this article, I am going to write t-sql query to concatenate two strings and concatenate string and Int in SQL server.

You can concatenate or combine two or more string values using + operator in SQL Server.
The + operator returns a string that is the result of concatenating two or more string values.

Declare @str1 varchar(50) ='hello'
Declare @str2 varchar(50) ='world'
SELECT (@str1+' '+@str2) as Result

You can also concatenate two different data type values using + operator. But you need to convert the all the values into single data type before concatenate the values.

Concatenate string and Int as string value in SQL server

Declare @str1 varchar(50) ='hello'
Declare @int1 int =10
SELECT (@str1+' '+cast(@int1 as varchar(50))) as Result

Concatenate string and Int as integer value in SQL server

Declare @str1 varchar(50) ='100'
Declare @int1 int =10
SELECT (cast(@str1 as int)+@int1) as Result

Note:

If you are using SQL Server 2012 and later versions, you can concatenate or combine two or more string values using CONCAT function.
The CONCAT function returns a string that is the result of concatenating two or more string values.

SELECT CONCAT ( 'Happy ', 'Birthday ', 11, '/', '25' ) AS Result;

You will get below error, if your run this query in older version of SQL Server like SQL Server 2008 r2.

Msg 195, Level 15, State 10, Line 1
'CONCAT' is not a recognized built-in function name.
Advertisement

Leave a Comment