Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Is there an easy way to count characters in a string as follows:count occurence of '-'qee-h8787-hjw-ooo-p would return 4qee-h8787-hjw would return 2Thnx
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 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
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!
sherrys
Starting Member
37 Posts
Posted - 2010-04-13 : 07:08:40
Great, thanks. I always come here for answers.
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)='-'