Skip to content
Advertisement

SQL count rows where column value contains string

I want to count the number of rows where a certain column has a substring in the column value.

This doesn’t work

Example: I want to count the rows including ‘no’

Expected result: 2

Because ‘no’ is a substring in ‘noa’ and ‘noit’.

Advertisement

Answer

Use a case expression:

Or, if that is the only value you want, use filtering and count(*):

The first is more suitable when you have additional columns in the query.

I should note that the SQL standard has another method of accomplishing this, using filter:

However, most databases do not support this syntax.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement