Summing up values in a varchar field.

  • Hi There,

    I have a problem in summing up the values exist in a varchar field for one of the table.

    The varchar field has got different values in different rows say for example:

    10 in first row,

    20 in second row and

    30.12 in third row.

    When i attempt to sum up the values i get an error converting the values.

    Can any one quickly help me in solving this issue either by converting all the decimal values into integer so that issue will be solved or by ignoring or substituting the improper values with a zero (0) to get the sum value ?

    Thanks in advance and looking forward for all your solutions.

    Subhash

  • What error do you get? It might be because there are non-numeric values in the column.

    Try this

    SELECT SUM(CAST(col AS FLOAT)) FROM tbl WHERE ISNUMERIC(col)=1

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks for the quick reply, it works.

    Will let you know again if i find any problem.

     

    Subhash

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

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