Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-15
6
Medium Priority
?
1,883 Views
Last Modified: 2014-04-15
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
0
Comment
Question by:TBSupport
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:Dulton
ID: 40002073
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

0
 
LVL 1

Author Comment

by:TBSupport
ID: 40002113
Thanks, Dulton!

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

Incorrect syntax near the keyword 'AS'.

TBSupport
0
 
LVL 6

Accepted Solution

by:
Dulton earned 2000 total points
ID: 40002120
My bad, I had a bogus comma in there.

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

Open in new window

0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 22

Expert Comment

by:plusone3055
ID: 40002176
x
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40002183
>> 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
 
LVL 1

Author Comment

by:TBSupport
ID: 40002344
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

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question