How to Concatenate Numbers and Strings to Format Numbers in T-SQL?
Change this:
SET @ActualWeightDIMS= @Actual_Dims_Lenght + 'x' +
@Actual_Dims_Width + 'x' + @Actual_Dims_Height;
To this:
SET @ActualWeightDIMS= CAST(@Actual_Dims_Lenght as varchar(3)) + 'x' +
CAST(@Actual_Dims_Width as varchar(3)) + 'x' +
CAST(@Actual_Dims_Height as varchar(3));
Change this:
SET @ActualWeightDIMS = @ActualWeight;
To this:
SET @ActualWeightDIMS = CAST(@ActualWeight as varchar(50));
You need to use CAST. Learn all about CAST and CONVERT here, because data types are important!
select 'abcd' + ltrim(str(1)) + ltrim(str(2))
A couple of quick notes:
- It's "length" not "lenght"
- Table aliases in your query would probably make it a lot more readable
Now onto the problem...
You need to explicitly convert your parameters to VARCHAR before trying to concatenate them. When SQL Server sees @my_int + 'X' it thinks you're trying to add the number "X" to @my_int and it can't do that. Instead try:
SET @ActualWeightDIMS =
CAST(@Actual_Dims_Lenght AS VARCHAR(16)) + 'x' +
CAST(@Actual_Dims_Width AS VARCHAR(16)) + 'x' +
CAST(@Actual_Dims_Height AS VARCHAR(16))
If you are using SQL Server 2012+ you can use CONCAT function in which we don't have to do any explicit conversion
SET @ActualWeightDIMS = Concat(@Actual_Dims_Lenght, 'x', @Actual_Dims_Width, 'x'
, @Actual_Dims_Height)