Solved

Sum CHAR DataType in SQL

Posted on 2004-10-13
7
2,810 Views
Last Modified: 2008-03-10
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
Comment
Question by:amedexitt
7 Comments
 
LVL 6

Expert Comment

by:RaisinJ
ID: 12303195
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
 
LVL 6

Assisted Solution

by:RaisinJ
RaisinJ earned 250 total points
ID: 12303255
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
 
LVL 5

Expert Comment

by:waelothman
ID: 12303368
select sum(cast (fieldname as real)) from tablename
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 1

Expert Comment

by:AntonG
ID: 12305393
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
 
LVL 7

Expert Comment

by:sukumar_diya
ID: 12305473
You can try this also

select sum(cast(columnname as float)) from tablename
0
 
LVL 8

Accepted Solution

by:
MartinCMS earned 250 total points
ID: 12308108
try...

select sum(convert(numeric(8,2),Field1))
from yourTable
0
 
LVL 5

Expert Comment

by:svid
ID: 12310369
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question