SELECT COUNT(DISTINCT) returns null when nothing is found instead of 0

  • SELECT COUNT(DISTINCT Component) AS Found FROM tblComponents WHERE(Component NOT LIKE '%[a-z]%') AND(LTRIM(RTRIM(Component)) = 'GM13622') GROUP BY Component

    How can I get the SELECT to return zero instead of null when there is no match?

  • SELECT ISNULL(blah blah, 0) AS Found ...


  • SELECT ISNULL(COUNT(DISTINCT Component), 0) AS Found FROM tblComponents WHERE(Component NOT LIKE '%[a-z]%') AND(LTRIM(RTRIM(Component)) = 'GM13622') GROUP BY Component

    I still get NULL or am I not understanding what you are suggesting?

  • Sorry. Try removing the GROUP BY

    SELECT Found = COUNT(DISTINCT Component)
    FROM tblComponents
    WHERE (Component NOT LIKE '%[a-z]%')
    AND (LTRIM(RTRIM(Component)) = 'GM13622');

  • Thx! That worked.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply