wants to add an integer value to a column which is char type.

  • Hi

    I have a column with datatype char(2). I want to add a fixed number say 7 to each of its record and store it as new column. Something like this:

    Col_original Col_new

    007

    018

    029

    0310

    etc

    something like.. i want to perform arithmetic operation on char column which in this case is addition.

    I think i should be able to convert 'Col_original' to int type AS

    Col_original

    0

    1

    2

    3

    Then , addition should be easy.

    Please suggest,

    Thanks

  • What issues are you running into in your attempt that you need help with?

    declare @C char(2),

    @d char(2)

    set @C = '01'

    set @d = '10'

    select @C + 7, @D + 7

    Results:

    8 | 17

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Yes you can do what you are planning and pretty simply as long as all the character values CAN be converted to integer. I'd do it something like this:

    Update table

    Set int_column = Convert(int, char_column) + 7

    Where

    -- only do it to covertable columns

    IsNumeric(char_column) = 1

    Then I can look for Nulls in the int_column which will help me find and fix inaccurate data in the char_column. The only issue with IsNumeric is that is considers '.', '+', '-', and currency symbols to be numeric so you could still get some errors.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply