tsql - Need to count specific letters from a search, why does it show as zero? the data is formatted as ntext -


===click me image of code== need count (3 example on first row) shows zero, wrong code, ntext why casted nvarchar

i trying count occurrence of letter 'a' on column named description, using query below getting result of zero.

select description, ( datalength(description) - datalength(replace(cast(description nvarchar(max)), 'a', '')) ) /datalength(description) [a]

from categories

an int divided int return int. notice *1.0

example

declare @description nvarchar(max)='cat in hat never seen'  select @description, (( datalength(@description) - datalength(replace(cast(@description nvarchar(max)), 'a', '')) )*100) / datalength(@description) [a] 

returns

(no column name)                cat in hat never seen   10 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -