Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do you join numbers in sql server 2008


example


SQL
SET @FIRSTNO= 400

SET @SECONDNO=12


SET @JOINNO= FIRSTNO+''+SECONDNO

expectation         =40012 



is the above the right way


Please advice

Thanks

What I have tried:

I have checked my own programs and the internet
(shouting removed)
Posted
Updated 7-Mar-16 4:33am
v3
Comments
Herman<T>.Instance 7-Mar-16 10:01am    
SET @JOINNO = @FirstNo * 100 + @SecondNo

If you want a string result:
SQL
DECLARE @FIRSTNO INT
DECLARE @SECONDNO INT
DECLARE @JOINNO VARCHAR(MAX)
 
SET @FIRSTNO= 400
SET @SECONDNO=12

SET @JOINNO= CAST(@FIRSTNO AS NVARCHAR) + CAST (@SECONDNO AS NVARCHAR)

If you want an integer:
SQL
DECLARE @FIRSTNO INT
DECLARE @SECONDNO INT
DECLARE @JOINNO INT
 
SET @FIRSTNO= 400
SET @SECONDNO=12

SET @JOINNO= CAST(CAST(@FIRSTNO AS NVARCHAR) + CAST (@SECONDNO AS NVARCHAR) AS INT)
 
Share this answer
 
If they are of INT type then you would want to convert to VARCHAR() first otherwise they will be added (as in Math addition). For example:
SQL
DECLARE @First INT, @Second INT
SELECT @First = 1, @Second = 2
SELECT CONVERT(VARCHAR(50), @First) + CONVERT(VARCHAR(50), @Second) 


Or you could use CONCAT()
SQL
SELECT CONCAT(@First, @Second)

which will convert them to strings for you.
 
Share this answer
 
Comments
jaket-cp 7-Mar-16 11:20am    
Just a small point, the CONCAT function becomes available in SQL Server 2012+
ZurdoDev 7-Mar-16 11:23am    
Good catch. I thought it had been there for a long time.
jaket-cp 7-Mar-16 11:25am    
It sticks in my head as the first time I saw it, I was using 2008 to try it out, with no luck :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900