Home All Groups Group Topic Archive Search About

count occurrence of character in a field using SQL

Author
10 Mar 2006 7:25 PM
Scott
Is there a function that will enable me to count the number of instances a
particular character is in a feild?  For instance...if I have a field named
number with a record with characters such as 00000111100000 and I what to do
a function that tells me the number of times the number 1 shows up in the
number field of that record.  The result would be 4.  Is this possible?

Author
10 Mar 2006 7:32 PM
Aaron Bertrand [SQL Server MVP]
SELECT LEN(column) - LEN(REPLACE(column, '1', '')) FROM table;




Show quote
"Scott" <Sc***@discussions.microsoft.com> wrote in message
news:819C28E2-76E5-4392-BA01-A7BDDF2BA57A@microsoft.com...
> Is there a function that will enable me to count the number of instances a
> particular character is in a feild?  For instance...if I have a field
> named
> number with a record with characters such as 00000111100000 and I what to
> do
> a function that tells me the number of times the number 1 shows up in the
> number field of that record.  The result would be 4.  Is this possible?
Author
10 Mar 2006 7:40 PM
Scott
Perfect!  Thank you!

Show quote
"Aaron Bertrand [SQL Server MVP]" wrote:

> SELECT LEN(column) - LEN(REPLACE(column, '1', '')) FROM table;
>
>
>
>
> "Scott" <Sc***@discussions.microsoft.com> wrote in message
> news:819C28E2-76E5-4392-BA01-A7BDDF2BA57A@microsoft.com...
> > Is there a function that will enable me to count the number of instances a
> > particular character is in a feild?  For instance...if I have a field
> > named
> > number with a record with characters such as 00000111100000 and I what to
> > do
> > a function that tells me the number of times the number 1 shows up in the
> > number field of that record.  The result would be 4.  Is this possible?
>
>
>

AddThis Social Bookmark Button