Combine varchar and int results together


We receive an alphanumeric string of 14 characters of data from our customers and we need to increment the last four numeric digits. The string is in the form of AB345678928976. We use the last four digits in a variable and work on it this way. Which works fine as I have set the variable as type INT. The problem is, we need now to combine them back together with the new last 4 digits and the original first 10 digits. So if we had done this on the example above, the last four digits would be different.

Any help would be appreciated.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
You'll have to CAST the int as a varchar in order to concatenate it with another varchar.

Something like...
Declare @val varchar(25) = 'AB34567892', @int int = 8976
SELECT @val + CAST(@int + 1 as varchar(4))

Open in new window

select CONCAT('AB34567892', 8976)

Jim's answer is absolutely fine, works in all sql server versions, this is just an alternative. a few nice things about CONCAT():

Available from SQL 2012 on, you can use the handy dandy CONCAT()

a. it will do the conversion for you
b, it handles NULLs gracefully
c. it is "sql standard" so code is more transportable (in theory, i'd not worry over this too much)
Morpheus7Author Commented:
Many thanks for the replies.

To do the work, I have split the original varchar string into 2 variables, one is the last four which will always be digits, but the original first ten characters are in a varchar variable. So being able to recombine them after the work is done on the last four back into a varchar column is proving tricky
Scott PletcherSenior DBACommented:
Seems straightforward:

SELECT LEFT(key_column, LEN(key_column) - 4) + CAST(RIGHT(key_column, 4) + 1 AS varchar(4))
    SELECT 'AB345678928976' AS key_column
) AS test_data

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

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.