Evidentally, I can’t do this:
SELECT
SUM( t.MyField IS NULL ) AS totalNulls,
SUM( t.MyField LIKE '[0-9]') AS totalNumbers
FROM MyTable AS t;
I don’t know why these don’t work, since booleans in SQL are just numbers (0 and 1). But the errors I’m getting suggest it is not legal to have ‘is null’ or ‘like’ anywhere in the select clause. Why aren’t they legal there? How do I achieve the intended effect, as suggested by the (pseudo) SQL above?
If your data isn’t indexed on the queried column you should use the CASE-based solution Aaron recommended because the rows will only be queried once.
You should know that you only accept e.g. ‘7’ as a number not ’12’ in this case.
If you want to accept any numbers you’d have to ask
In this case you could do a charm using isnumeric:
If the column is indexed a variation of Michaels solution could be fastest:
There are so many ways …