• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3715
  • Last Modified:

Sum CHAR DataType in SQL

How can I Sum a CHAR DataType in SQL????????
The Data that I have in my table is like this
Field1
---------
1400.00
1521.00
1523.35
.
.
.
Even it looks like number it is not it was specified as CHAR in the Tbale's Dising
0
amedexitt
Asked:
amedexitt
2 Solutions
 
RaisinJCommented:
Convert(INT,@Field)

Select
    Sum(Convert(INT,Field1)) As Field1
From
    Table!

The only time this will give you a problem is if there are non numeric charaters in the field...
0
 
RaisinJCommented:
Sorry,

Noticed your Fields has decimals... In that case, you'll receive and error when converting to INT, you want to Convert to Decimal...

Convert(Decimal(10,2),Field)

0
 
waelothmanCommented:
select sum(cast (fieldname as real)) from tablename
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AntonGCommented:
although the above comments are pretty much correct.... I would rather do the conversion to a float...

i.e. SELECT SUM(CONVERT(float, colname)) FROM tablename
0
 
sukumar_diyaCommented:
You can try this also

select sum(cast(columnname as float)) from tablename
0
 
MartinCMSCommented:
try...

select sum(convert(numeric(8,2),Field1))
from yourTable
0
 
svidCommented:
To be on the safer side you may want to add

where isnumeric(Field1) = 1 to the select statement so you don't get a syntax error if there are non-numeric values
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now