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.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Perfect Thanks

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

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