To count the number of vowels in a given string in SQL Server, you can use a combination of CHARINDEX and WHILE loop
Example,
/* Count a vowels from a given string in sql server */
DECLARE @stringHello VARCHAR(1000) ='Hello world example'
DECLARE @vowel TABLE(
id INT IDENTITY(1,1) ,
vowels VARCHAR(1)
)
INSERT INTO @vowel SELECT 'a'
INSERT INTO @vowel SELECT 'e'
INSERT INTO @vowel SELECT 'i'
INSERT INTO @vowel SELECT 'o'
INSERT INTO @vowel SELECT 'u'
DECLARE @i INT=1
DECLARE @count INT = (SELECT COUNT(1) FROM @vowel)
DECLARE @vowelCount INT=0
WHILE(@i <=@count)
BEGIN
IF(
SELECT CHARINDEX(
(SELECT vowels FROM @vowel WHERE id= @i)
,@stringHello
)
)>0
BEGIN
SET @vowelCount = @vowelCount +1
END
SET @i =@i+1
END
SELECT @vowelCount AS VowelsCharCount