473,378 Members | 1,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

SQL null problem

I am writing a query in ASP.NET C#,

select sum(size) as total_size from my_table where is_valid = 1

However, if there is no data in the database, it returns 1 record with
value null. How come it doesn't return 0 instead? because I am using
"sum", if SQL can't find any record.. isn't it suppose to return 0?

In my table setup, the default value for the "size" field is 0 and doesn't
allow NULL.

Regards,

T.
Nov 16 '05 #1
3 5342
Tommi,

The reason this happens is that most (I believe) operations in sql will
return null when one of the operands is null. You have to account for this.
Basically, you want to do:

select isnull(sum(size), 0) as total_size from my_table where is_valid = 1

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Tommi Bauer" <mo****@hotmsn.com> wrote in message
news:J4********************@news04.bloor.is.net.ca ble.rogers.com...
I am writing a query in ASP.NET C#,

select sum(size) as total_size from my_table where is_valid = 1

However, if there is no data in the database, it returns 1 record with
value null. How come it doesn't return 0 instead? because I am using
"sum", if SQL can't find any record.. isn't it suppose to return 0?

In my table setup, the default value for the "size" field is 0 and doesn't
allow NULL.

Regards,

T.

Nov 16 '05 #2
Hi Tommi,

"Tommi Bauer" <mo****@hotmsn.com> wrote in message
news:J4********************@news04.bloor.is.net.ca ble.rogers.com...
I am writing a query in ASP.NET C#,

select sum(size) as total_size from my_table where is_valid = 1

However, if there is no data in the database, it returns 1 record with
value null. How come it doesn't return 0 instead? because I am using
"sum", if SQL can't find any record.. isn't it suppose to return 0?

In my table setup, the default value for the "size" field is 0 and doesn't
allow NULL.

Regards,


That's the defined behavior of the SQL SUM function. It make sense
because you really can't some "nothing". It also allows you to distinguish
between having n number of values that sum to 0 and having 0 values.
Assuming you are querying SQL Server, you could write the query thus to
remove the null:

select isnull(sum(size),0) as total_size from my_table where
is_valid = 1

By the way, this is definitely more of a SQL question than an ASP.NET or
C# question. The microsoft.public.sqlserver.programming newsgroup is a good
choice for questions on this topic.

Regards,
Daniel
Nov 16 '05 #3
Thanks a lot Nicholas and Daniel. Really appreciate your solutions and
explanation, it works!


"Daniel Pratt" <ko******************@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi Tommi,

"Tommi Bauer" <mo****@hotmsn.com> wrote in message
news:J4********************@news04.bloor.is.net.ca ble.rogers.com...
I am writing a query in ASP.NET C#,

select sum(size) as total_size from my_table where is_valid = 1

However, if there is no data in the database, it returns 1 record with
value null. How come it doesn't return 0 instead? because I am using
"sum", if SQL can't find any record.. isn't it suppose to return 0?

In my table setup, the default value for the "size" field is 0 and doesn't allow NULL.

Regards,
That's the defined behavior of the SQL SUM function. It make sense
because you really can't some "nothing". It also allows you to distinguish
between having n number of values that sum to 0 and having 0 values.
Assuming you are querying SQL Server, you could write the query thus to
remove the null:

select isnull(sum(size),0) as total_size from my_table where
is_valid = 1

By the way, this is definitely more of a SQL question than an ASP.NET

or C# question. The microsoft.public.sqlserver.programming newsgroup is a good choice for questions on this topic.

Regards,
Daniel

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sascha | last post by:
Hi I need some help on achieving the following: I wrote a querie which collects Data out of three Tables, the Result looks like this: SET NOCOUNT ON DECLARE @ROW INT
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
7
by: am72de | last post by:
Hi all, I've posted this problem some weeks ago, but noone had a solution. Perhaps now someone could help me. I have the following tables: Create Table T1 ( ID1 int Not Null , ID2 int Not...
27
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
3
by: Jon L | last post by:
Hi, I'm hoping someone can help me with this problem. I'm not sure whether the problem lies with the software or with my understanding of the language. I'm using the Microsoft.XMLDOM object...
1
by: connor7777 | last post by:
Hi guys: We've been weeding out errors off of a java->c# project and have managed to redeem most of our code with the exception of one bug that we for some reason cannot pin down. The following...
4
by: Debbiedo | last post by:
I searched the groups and tried several approaches but still cannot find a solution. I have a table that has several hundred fields that may or may not need to be displayed in a report,...
8
by: A. Anderson | last post by:
Howdy everyone, I'm experiencing a problem with a program that I'm developing. Take a look at this stack report from GDB - #0 0xb7d782a3 in strlen () from /lib/tls/i686/cmov/libc.so.6 #1 ...
0
prabirchoudhury
by: prabirchoudhury | last post by:
CRITERIA; +-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.