Sum of a VarChar Field

How to I perform a sum on a varchar field that always contains numeric content?
LVL 2
E-RiskAsked:
Who is Participating?
 
DireOrbAntConnect With a Mentor Commented:
SUM(CAST(MyVarcharField AS INT))
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT SUM(CAST (varcharColumn as integer )) -- you can cast it as integer or numeric (10,2) depending on the values stored
WHERE ISNUMERIC(varcharColumn) =1
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
Now , if it stores a numeric field always, they ehy you keep it as a varchar column ?
0
 
Raynard7Commented:
select sum(cast(fieldName as int)) from tableA

Will find the sum (assumin that the numbers you have are integers)
0
All Courses

From novice to tech pro — start learning today.