• Status: Solved
• Priority: Medium
• Security: Public
• Views: 2054

# T-SQL: Concatenating two numbers--not adding them

Hello:

Below is my T-SQL script where I'm trying to take the last four digits of a Social Security Number and append with the year of that employees birthdate.

select right(UPR00100.SOCSCNUM, 10)+datepart(yy, UPR00100.BRTHDATE) from UPR00100

What do I need to do to get the script to simply show me the two numbers combined and not added?

Thanks!

TBSupport
0
TBSupport
1 Solution

Commented:
you need to cast/convert the numbers to a string (varchar) in order to concatenate.

``````select Cast(right(upr00100.socscnum,10), AS VARCHAR(10)) + Cast(Year(upr00100.brthdate) AS VARCHAR(4)) as [MyField] FROM upr00100
``````
0

Author Commented:
Thanks, Dulton!

I'm getting the following error, upon running this:

Incorrect syntax near the keyword 'AS'.

TBSupport
0

Commented:

``````select Cast(right(upr00100.socscnum,10) AS VARCHAR(10)) + Cast(Year(upr00100.brthdate) AS VARCHAR(4)) as [MyField] FROM upr00100
``````
0

Commented:
x
0

Senior DBACommented:
>> I'm trying to take the last four digits of a Social Security Number and append with the year of that employees birthdate. <<

SELECT right(UPR00100.SOCSCNUM, 4) + convert(char(4), UPR00100.BRTHDATE, 112)
from UPR00100
0

Author Commented:
Dulton is close.  But, it is not concatenating the two fields.  It has approximately four spaces between them.

Here is the revised and correct solution:

select rtrim(Cast(right(upr00100.socscnum,10) AS VARCHAR(10)))
+ Cast(Year(upr00100.brthdate) AS VARCHAR(4)) as test from UPR00100

Thanks, Dulton!

TBSupport
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.