Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Counting characters

Author  Topic 

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2007-12-11 : 11:42:31
Is there an easy way to count characters in a string as follows:

count occurence of '-'

qee-h8787-hjw-ooo-p would return 4

qee-h8787-hjw would return 2

Thnx

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-12-11 : 11:45:53
declare @str varchar(100)
select @str = 'qee-h8787-hjw-ooo-p'
select len(@str) - len(replace(@str, '-', ''))

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2007-12-11 : 11:47:35
OMG!!!

Now that is one hell of a quick reply, and kool too :)

Thanks a million!
Go to Top of Page

sherrys
Starting Member

37 Posts

Posted - 2010-04-13 : 07:08:40
Great, thanks. I always come here for answers.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-04-13 : 07:51:56
The hard way

declare @str varchar(100)
select @str = 'qee-h8787-hjw-ooo-p'
select count(substring(@str,number,1)) from master..spt_values where type='p'
and number between 1 and len(@str)and substring(@str,number,1)='-'


PBUH
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-04-13 : 07:59:49
Wow this post is 3 years old !!!

PBUH
Go to Top of Page
   

- Advertisement -