sql server - Boolean Logic in Select Clause -


evidentally, can't this:

select    sum( t.myfield null ) totalnulls,    sum( t.myfield '[0-9]') totalnumbers mytable t; 

i don't know why these don't work, since booleans in sql numbers (0 , 1). errors i'm getting suggest not legal have 'is null' or 'like' anywhere in select clause. why aren't legal there? how achieve intended effect, suggested (pseudo) sql above?

if data isn't indexed on queried column should use case-based solution aaron recommended because rows queried once.

select  totalnulls = sum(case when myfield null 1 else 0 end),         totalnumbers = sum(case when myfield '[0-9]' 1 else 0 end)    mytable; 

you should know accept e.g. '7' number not '12' in case. if want accept numbers you'd have ask

... when myfield not '%[^0-9]%' ... 

in case charm using isnumeric:

select  totalnonnumbers = count(*) - sum(isnumeric(myfield)),         totalnumbers = sum(isnumeric(myfield))    mytable; 

if column indexed variation of michaels solution fastest:

select  totalnulls = (select count(*) mytable myfield null),         totalnumbers =            (select count(*) mytable myfield between '0' , '9'); 

there many ways ...


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -