?
Solved

Sum CHAR DataType in SQL

Posted on 2004-10-13
7
Medium Priority
?
3,411 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 1000 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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 1000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

649 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