Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i just wanted find the how many times b has occurred.
Can you please explain this query. it would be helpful
Thanks !

What I have tried:

INSERT INTO tblname
values ('abbcbbcbbb')

select name, len(name)-len(replace(name,'b',''))/len('b') from #temp
Go
--7
Posted
Updated 3-Sep-18 15:11pm
Comments
Mohibur Rashid 3-Sep-18 20:29pm    
Logic seems ok, what is the problem?

1 solution

Just break it down bit by bit...

len(name) = 10
replace(name,'b','') = 'acc'
len('acc') = 3
10 - 3 = 7

Basically, your "replace" removes all of the letters 'b'. So, all that remains are the letters that are NOT 'b'. The difference in length between the original (which includes 'b') and replacement (which does not include 'b') is the number of 'b's.

The divide by len('b') is unnecessary...since, that is simply 1...and dividing by 1 does nothing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900