Database & SQL

Moderators: None (Apply to moderate this forum)
Number of threads: 1194
Number of posts: 2247

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
count() Posted by wannabedba on 18 Nov 2004 at 12:33 PM
i want to count and display the number of order numbers associated with each state. some states do not have any orders and therefore are null, and i need to display every state. so when i do a select count(order_num), state_code from ...... the states that have no orders are null and not displayed. can i solve this matter in my select statement, or will i have to do it in a procedure?
Report
Re: count() Posted by Geoff-S on 19 Nov 2004 at 2:41 AM
: i want to count and display the number of order numbers associated with each state. some states do not have any orders and therefore are null, and i need to display every state. so when i do a select count(order_num), state_code from ...... the states that have no orders are null and not displayed. can i solve this matter in my select statement, or will i have to do it in a procedure?
:
Hi,
It depends where you are running this SQL Statement. If you are doing it in a VB Code module then you will have to test for Null on the return value and replace it with zero.
If, however, you are running this in a SQL-Server Stored Procedure then you can use "IsNull" in the Select statement - SELECT @Count = IsNull( (count.......) , 0)
Whatever you place in the second part after the comma is returned if the firstpart is null. In your case you want it to be Zero.


In a World full of blind men the one-eyed man is King

Report
Re: count() Posted by wannabedba on 19 Nov 2004 at 7:01 AM
: : i want to count and display the number of order numbers associated with each state. some states do not have any orders and therefore are null, and i need to display every state. so when i do a select count(order_num), state_code from ...... the states that have no orders are null and not displayed. can i solve this matter in my select statement, or will i have to do it in a procedure?
: :
: Hi,
: It depends where you are running this SQL Statement. If you are doing it in a VB Code module then you will have to test for Null on the return value and replace it with zero.
: If, however, you are running this in a SQL-Server Stored Procedure then you can use "IsNull" in the Select statement - SELECT @Count = IsNull( (count.......) , 0)
: Whatever you place in the second part after the comma is returned if the firstpart is null. In your case you want it to be Zero.
:
: im using this in powerbuilder and its accessing a sql server db
: In a World full of blind men the one-eyed man is King
:
:

Report
Re: count() Posted by Geoff-S on 19 Nov 2004 at 7:29 AM
: : : i want to count and display the number of order numbers associated with each state. some states do not have any orders and therefore are null, and i need to display every state. so when i do a select count(order_num), state_code from ...... the states that have no orders are null and not displayed. can i solve this matter in my select statement, or will i have to do it in a procedure?
: : :
: : Hi,
: : It depends where you are running this SQL Statement. If you are doing it in a VB Code module then you will have to test for Null on the return value and replace it with zero.
: : If, however, you are running this in a SQL-Server Stored Procedure then you can use "IsNull" in the Select statement - SELECT @Count = IsNull( (count.......) , 0)
: : Whatever you place in the second part after the comma is returned if the firstpart is null. In your case you want it to be Zero.
: :
: : im using this in powerbuilder and its accessing a sql server db
: :
: :
Have not use powerbuilder so I'm not sure what your return is, presumably you feed the return to a variable, so you just need to put a line of code in that says :
If IsNull(varReturn) then
varReturn = 0
End If
However, if you are running it as a whole batch and retrieving a Recordset then presumably you are missing entries for those with no count, so you could either get a list of state-code then loop through one at a time so that you can make the above test, or construct the SQL String with the IsNull in it and then pass that to sp_executesql to return a full recordset.



In a World full of blind men the one-eyed man is King




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.