How can I count fields that are NULL?

2

I'm using this query: I would like to know how I can count these fields that have no records. Here it returns 0. But I have some null records. What do I do?

SELECT DataFim, COUNT(DataFim) AS QTD FROM AtivacaoGuincho
GROUP BY DataFim
HAVING DataFim is  null
    
asked by anonymous 26.01.2017 / 18:00

2 answers

8

So:

SELECT COUNT(*) AS QTD FROM AtivacaoGuincho
WHERE DataFim is  null

Since Marlon has touched on the subject of line counting, I can little more this response and get into a subject that is almost a curiosity.

The count is basically used in this way. I ask you to count all rows where DATAFIM is null.

But could do so:

SELECT COUNT(*) AS TOTAL, COUNT(DATAFIM) AS DTFIM FROM AtivacaoGuincho

And it would have something like this: I have 1884 rows in the table and 50 rows where DATAFIM is null.

Of course, I could have as many counts as I wanted in my select.

    
26.01.2017 / 18:03
7

The where clause was missing correctly.

SELECT COUNT(DataFim) AS QTD 
FROM AtivacaoGuincho
WHERE DataFim is  null

EDIT:

Sorry for the error of Ctrl+c/Ctrl+v , the correct one is:

SELECT COUNT(*) AS QTD 
FROM AtivacaoGuincho
WHERE DataFim is null

And for those who do not know the difference between Count(*) and Count(coluna) is that:

  • Count(*) counts all lines without exceptions.
  • Count(coluna) counts values of coluna that are not nulls (% with%)
26.01.2017 / 18:03