Add two varchar columns which has numeric data

  • I have two varchar columns, but I have numeric data in both the columns.

    Is it possible to add two columns?

    Please let me know

    Thanks

  • SQLAddict01 (2/10/2016)


    I have two varchar columns, but I have numeric data in both the columns.

    Is it possible to add two columns?

    Please let me know

    Thanks

    Sure. Here's a way:

    DECLARE @x varchar(10) = '19.77'

    DECLARE @y varchar(10) = '12.333'

    SELECT res = try_cast(@x AS decimal(19,6)) + try_cast(@y AS decimal(19,6))

    If either of the columns contains non-numeric data, the result will be NULL.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Perfect Thanks

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

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