Combine varchar and int results together

Posted on 2014-08-15
Last Modified: 2014-09-01

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.

Question by:Morpheus7
    LVL 65

    Expert Comment

    by:Jim Horn
    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

    LVL 47

    Expert Comment

    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)

    Author Comment

    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
    LVL 68

    Accepted Solution

    Seems straightforward:

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

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now