Question

I have string like this 'SUZUKI GSX_1300/TWOWHEELER'

Now I want to split the above string into words and inserting the one by one word into one table.

DECLARE @a TABLE (a nvarchar(500));
declare @RTADESC nvarchar(100) = 'SUZUKI GSX_1300/TWOWHEELER';
declare @b int =1;
declare @c int;
set @c=CHARINDEX('_',@RTADESC,@b);

WHILE @c>0
        BEGIN
            SELECT @b= CHARINDEX('_' ,@RTADESC,@b)+1
            INSERT INTO @a
            SELECT SUBSTRING(@RTADESC,0,@b-1)
            set @c=CHARINDEX('_',@RTADESC,@b)
        END

        SELECT * FROM @a

Now I want to see the data in the table @a as below

SUZUKI
GSX
1300
TWOWHEELER

Based on it I will search for vehicle makers in my table.

So please help on this how I can split my string I may get different special characters as well in future.

Was it helpful?

Solution

There are some excellent options available at: http://www.sqlperformance.com/2012/07/t-sql-queries/split-strings

There are solutions which allow you to pass the delimiter also, so you could pass in different ones in future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top