Find all word From a String…..

Today I will discuss how we can found  all word individually from a string through SQL script.

Suppose we have a string “This Is an Example String” ..
So,We need the output like this:

—-Output—-
This
Is
an
Example
String
————–

Here is the SQL script for this:

DECLARE @Str nvarchar(2000);
Declare @cut nvarchar(200);

BEGIN
SET @Str=’This Is an Example String’
SET @Str=@Str+’ ‘

WHILE (CHARINDEX(‘ ‘, @Str)>1)
BEGIN
set @cut= RTRIM(LTRIM (SUBSTRING(@Str,0,CHARINDEX(‘ ‘, @Str))));
set @Str=LTRIM(REPLACE(@Str,@cut,”))

print @cut;

End
END