Link to home
Start Free TrialLog in
Avatar of TBSupport
TBSupport

asked on

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

Instead, this script is mathematically adding the two numbers together.

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

Thanks!

TBSupport
Avatar of Dulton
Dulton

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

Open in new window

Avatar of TBSupport

ASKER

Thanks, Dulton!

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

Incorrect syntax near the keyword 'AS'.

TBSupport
ASKER CERTIFIED SOLUTION
Avatar of Dulton
Dulton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scott Pletcher
>> 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
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