Why would you ever use count ('foo')?

I've looked at the difference between:

count(*)
count(column_name)
count(1)

      

For example: here: Count (*) vs Count (1) . Some respondents also state: count ('foo'). Why would you ever use count ('foo'). This seems to work:

select count('hello my name is joe bloggs and I finished school about ten years ago') from dbdisposals

      

Why would you need to pass a string to the count function?

+3


source to share


1 answer


Using any literal value in count

has the same effect as count(*)

, it counts the number of rows in a group. Usage count('str')

has the same effect as count(1)

, i.e. Counts records where the value is not equal null

. Since there will be no literal value ( 'str'

or 1

) null

for any of the entries, it counts all entries.

The function count

counts all nonzero values, so literal value in count

has the same effect as count(*)

, but count(column_name)

only counts nonzero values ​​in that column.



Using a string literal in count

works great, the only reason you avoid it is to be confusing.

+3


source







All Articles