Saturday, April 11, 2015

Number Of VOWELS Exists in a Given String by SQL Query in SSMS


To solve this scenario, we have to declare some variables like counter,string location counter, Input string container and string container after split etc. Then get the length of string and create a loop. After that we have to split the string by each single alphabet and compare with ‘AEIOU’. If the condition fulfilled then increase the count by 1. Last display the value of Count.


Here is the code:
DECLARE @INPUTED_STRING NVARCHAR(MAX)='Pritam Das'    --String have to input
DECLARE @Str_loc INT=1 --This is string position Locator
DECLARE @COUNT INT=0 --Output variable
DECLARE @STRING_CHAR_CON NVARCHAR(MAX) --This will contain the character of string
WHILE @Str_loc <= LEN(@INPUTED_STRING)
BEGIN
SET @STRING_CHAR_CON = SUBSTRING(@INPUTED_STRING, @Str_loc, 1)
IF @STRING_CHAR_CON = 'A'
OR @STRING_CHAR_CON = 'E'
OR @STRING_CHAR_CON = 'I'
OR @STRING_CHAR_CON = 'O'
OR @STRING_CHAR_CON = 'U'
BEGIN
SET @COUNT = @COUNT + 1
END
SET @Str_loc = @Str_loc + 1
END

Select @COUNT AS TotalNoOfVowels

Output:   TotalNoOfVowels
                3

No comments:

Post a Comment