Solved

Sum CHAR DataType in SQL

Posted on 2004-10-13
7
3,112 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

690 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