Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 .Net type associated to SQL SUM ?

Author  Topic 

vermorel
Starting Member

26 Posts

Posted - 2006-05-05 : 04:18:52
I have an SQL query
SELECT SUM(DATALENGTH(Data)) FROM FooBar

where 'Data' is an 'image' field.

I am calling this query from .Net code, aka
 SqlCommand command = new SqlCommand(@"SELECT SUM(DATALENGTH(Data)) FROM FooBar", connection);
object result = command.ExecuteScalar();


I would like to know the expected .Net type of "result". Sometimes I get an 'int' and sometimes I get an other type causing cast exception if I do
int myValue = (int)result


Do anyone knows what is the expected .Net type returned by SUM ?

Thanks in advance,
Joannes

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-05-05 : 07:37:58
SUM does not have a datatype.

The datatype of the value you are summing is the datatype that SUM will return.


CODO ERGO SUM
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2006-05-05 : 08:47:00
Wrap a CAST clause around the SUM to convert it to a predictable number format every time.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-05-05 : 11:29:34
Watch out for Nulls, too ! Those will throw an exception no matter what datatype you attempt to convert them to ...
Go to Top of Page
   

- Advertisement -