Showing posts with label basically. Show all posts
Showing posts with label basically. Show all posts

Friday, March 9, 2012

How to return 0 instead of null when using a sum function?

Hi,

I basically do not want to return a null value as a result of using a sum function (using sum against 0 rows).

Is there a common way to avoid this?

Thanx

Interesting you should ask. I was looking at the same problemyesterday. My results are displayed in a DataGrid and thankfully itdoesn't barf on the NULL values. It displays -1 instead. Of coursethat's still not desirable as the correct answer for SUM(nothing)should be 0 IMO.
Let's hope someone has a suggestion.
|||You could use the ISNULL function to calculate a 0 whenever the SUM contains a NULL:
SELECT ISNULL(SUM(myColumn,0)) FROM myTable WHERE 0=1

|||

Or, as a slight variation, use ISNULL to replace the result of the SUM for instances where no rows result in an answer of NULL:

SELECT ISNULL(SUM(myColumn),0) FROM myTable WHERE 0 = 1

(Note the different parenthesis placement: tmorton's will replace any NULL value with zero BEFORE aggregation; mine will replace theentire result with 0 AFTER aggregation in the event of a NULL sum).

|||

pjmcb wrote:


Or, as a slight variation, use ISNULL to replace the result of the SUM for instances where no rows result in an answer of NULL:
SELECT ISNULL(SUM(myColumn),0) FROM myTable WHERE 0 = 1
(Note the different parenthesis placement: tmorton's will replace anyNULL value with zero BEFORE aggregation; mine will replace the entireresult with 0 AFTER aggregation in the event of a NULL sum).


Thank you for catching my typo pjmcb! What I actually posted is not even syntactically correct. :-)
I should have copied-and-pasted from Query Analyzer instead of retyping.



|||I didn't think you could do that, but I was too lazy to open up QA to test it out...

Friday, February 24, 2012

How to retrieve data between two dates

For e.g
I want the amount of a customer between 2nd March 2004 and 2nd October
2004.
Basically How to filter this as i cannot put it in a where clause.i.e
customer name in rows amount in columns.
Is this in SQL Server or AS?
If the former, please post DDL, sample data, and sample output.
http://www.aspfaq.com/etiquette.asp?id=5006
If the latter, I will let someone else handle this question
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
"Amit Asawa" <amit.asawa@.capgemini.com> wrote in message
news:814276fc.0412022212.7d57e2da@.posting.google.c om...
> For e.g
>
> I want the amount of a customer between 2nd March 2004 and 2nd October
> 2004.
> Basically How to filter this as i cannot put it in a where clause.i.e
> customer name in rows amount in columns.
|||If u have got solution to this problem can u please forward it to me at mailketna@.yahoo.com.
I have same problem in asp.net in crystal reports.
Thankyou
************************************************** ********************
Sent via Fuzzy Software @. http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
|||Can you post DDL, sample data, and sample output? I'll help you out, but
not via e-mail. Let's keep this public so that others can benefit from the
discussion as well.
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
"jalpa patel" <mailketna@.yahoo.com> wrote in message
news:u%23l$ihn4EHA.3388@.TK2MSFTNGP15.phx.gbl...
> If u have got solution to this problem can u please forward it to me at
mailketna@.yahoo.com.
> I have same problem in asp.net in crystal reports.
> Thankyou
> ************************************************** ********************
> Sent via Fuzzy Software @. http://www.fuzzysoftware.com/
> Comprehensive, categorised, searchable collection of links to ASP &
ASP.NET resources...

How to retrieve data between two dates

For e.g
I want the amount of a customer between 2nd March 2004 and 2nd October
2004.
Basically How to filter this as i cannot put it in a where clause.i.e
customer name in rows amount in columns.Is this in SQL Server or AS?
If the former, please post DDL, sample data, and sample output.
http://www.aspfaq.com/etiquette.asp?id=5006
If the latter, I will let someone else handle this question
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--
"Amit Asawa" <amit.asawa@.capgemini.com> wrote in message
news:814276fc.0412022212.7d57e2da@.posting.google.com...
> For e.g
>
> I want the amount of a customer between 2nd March 2004 and 2nd October
> 2004.
> Basically How to filter this as i cannot put it in a where clause.i.e
> customer name in rows amount in columns.|||If u have got solution to this problem can u please forward it to me at mail
ketna@.yahoo.com.
I have same problem in asp.net in crystal reports.
Thankyou
****************************************
******************************
Sent via Fuzzy Software @. http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET
resources...|||Can you post DDL, sample data, and sample output? I'll help you out, but
not via e-mail. Let's keep this public so that others can benefit from the
discussion as well.
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--
"jalpa patel" <mailketna@.yahoo.com> wrote in message
news:u%23l$ihn4EHA.3388@.TK2MSFTNGP15.phx.gbl...
> If u have got solution to this problem can u please forward it to me at
mailketna@.yahoo.com.
> I have same problem in asp.net in crystal reports.
> Thankyou
> ****************************************
******************************
> Sent via Fuzzy Software @. http://www.fuzzysoftware.com/
> Comprehensive, categorised, searchable collection of links to ASP &
ASP.NET resources...