Solved

COALESCE and ISNULL Functions Not Returning 0 Value

Posted on 2006-06-15
6
1,300 Views
Last Modified: 2012-08-13
Hi,

I'm using SQL Server 2000.  I would like my RecordCount column to return a 0 value.  The RecordCount is a numeric value.  I've tried the COALESCE and IsNull functions with no success as follows:


SELECT     COUNT(COALESCE (ItemCount, 0)) AS RecordCount
FROM         dbo.CAR_SummaryTable
GROUP BY ItemCategory
HAVING      (ItemCategory = 'Unincorp Central Region')


SELECT     COUNT(ISNULL(ItemCount, 0)) AS RecordCount
FROM         dbo.CAR_SummaryTable
GROUP BY ItemCategory
HAVING      (ItemCategory = 'Unincorp Central Region')

Any Suggestions?
Thanks,
Denise
0
Comment
Question by:DeniseGoodheart
  • 2
  • 2
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16915623

SELECT     ISNULL(COUNT(ItemCount), 0) AS RecordCount
FROM         dbo.CAR_SummaryTable
GROUP BY ItemCategory
HAVING      (ItemCategory = 'Unincorp Central Region')
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 40 total points
ID: 16915628
note that, when grouping by values, it will not produce rows with 0 at all if there is no row for the grouped by fields...
0
 
LVL 7

Accepted Solution

by:
ExpertAdmin earned 460 total points
ID: 16915760
I don't think you need a COALESCE or ISNULL at all if you are using a COUNT. Count will ALWAYS return a number, no matter what the value of the column is in the database. Therefore, this should work:

SELECT     COUNT(*) AS RecordCount
FROM         dbo.CAR_SummaryTable
WHERE    (ItemCategory = 'Unincorp Central Region'

Or am I missing the point?

M@
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:DeniseGoodheart
ID: 16915993
Hi ExpertAdmin,

No, I was missing the point.  I'm working on a project where the project due date is based on the budget due date, and not the actual time required for the project.  In any event, sometimes I lose perspective in what I'm doing because I'm trying to meet the aggressive project due date!  I am so thankful for Experts Exchange!

Many Thanks,
Denise
0
 

Author Comment

by:DeniseGoodheart
ID: 16916002
Hi angelIII:

Many thanks for your useful information!

Denise
0
 
LVL 7

Expert Comment

by:ExpertAdmin
ID: 16916039
Anytime. Glad I could help. I have been on a few of those projects myself.

M@
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question