Solved

# summing values that can be a null or float value

Posted on 2011-03-03
190 Views
I have a query where I sum a value sum(U_CPA_NUMER), this value can be a NULL or Float values.  If a null is one of the values my result is null. I have tried using a case statement,

sum(case when u_cpa_numer is null then 0 else u_cpa_numer end) but this yields a 0 value only.

I tried excluding null values for this field in my where clause but this wacks out the whole query.  Please help.
0
Question by:wilcon
• 9
• 5

LVL 9

Accepted Solution

joshbula earned 250 total points
SUM(ISNULL(U_CPA_NUMBER, 0)) AS U_CPA_NUMBER
0

LVL 13

Assisted Solution

Philip Pinnell earned 250 total points
try

sum(isnull(U_CPA_NUMER,0))
0

Author Comment

tried that as well still comes back with 0
0

LVL 13

Expert Comment

Interesting

try this

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[test]') AND type in (N'U'))
DROP TABLE [dbo].[test]
go
CREATE TABLE [dbo].[test](
[id] [smallint] IDENTITY(1,1) NOT NULL,
[val] [float] NULL
) ON [PRIMARY]

INSERT INTO [test]
([val])
VALUES
(2)
INSERT INTO [test]
([val])
VALUES
(3)
INSERT INTO [test]
([val])
VALUES
(Null)
INSERT INTO [test]
([val])
VALUES
(4)
INSERT INTO [test]
([val])
VALUES
(Null)

SELECT * from [test]
SELECT
sum([val]) as s1,
sum(isnull([val],0)) as s2
FROM [test]
0

LVL 13

Expert Comment

for me both sums return 9
0

Author Comment

is it possible it's because my values are of float datatype
0

LVL 13

Expert Comment

The example above is a float datatype.

When you run the above SQL do you get 9 for both sums?

I presume the correct sum for query is definitely not 0
0

Author Comment

Yes this case works.
0

LVL 13

Expert Comment

0

LVL 13

Expert Comment

can you post some example data from your table and the schema?
0

LVL 13

Expert Comment

If your where query only includes null values however the result will be null and the isnull(U_CPA_NUMER,0) version will be 0

This seems to describe your situation.

Please check the data or try with different criteria
0

Author Comment

There is definitely NULL and Float values.
0

LVL 13

Expert Comment

If you do

select U_CPA_NUMER
where yourwhereclause

select sum(U_CPA_NUMER)
where yourwhereclause

You do get none 0 values as well as nulls?
0

Author Comment

found the issue thanks!
0

LVL 13

Expert Comment

...and it was?
0

## Featured Post

### Suggested Solutions

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.