Here is a simple way to get a specific character count (for example, count the number of occurences of the character ‘0’) and the word count of a varchar string using T-SQL.
-- Count the number of specific characters, in this case, ‘0’
Declare
Set
Select
-- Count the number of words
DECLARE
SELECT
As you see this is quite straightforward, the script simply substract the length of the string minus the character searched from the full length of the string, giving as a result the character count. The script counting words is simply counting the space characters.
Other posts:
How to remove multiple whitespaces from a string with SQL Server 2005
How to generate random numbers with a SQL query
How to remove leading zeros from the results of an SQL Query
How to Find The List Of Unused Tables Since The Last SQL Server Restart
Which performs better: ISNULL or COALESCE
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.