SQL Query to Split string by specific seperator

On May 19

SQL Query to Split string by specific seperator

sql qUERY TO SPLIT STRING BY SPECIFIC SEPERATOR


DECLARE @start INT, @end INT ,@string varchar(200),@delimiter varchar(10)
    set @string='DIMAPUR,KIPHIRE,KOHIMA,LONGLENG ,MOKOKCHUNG,MON,PEREN,PHEK,TUENSANG,WOKHA,ZUNHEBOTO'
    set @delimiter=','
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1

    BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1
           --print  SUBSTRING(@string, @start, @end - @start)
           update [tbl_city] set state_id=21 where city_name=SUBSTRING(@string, @start, @end - @start)
           INSERT INTO tbl_city ([state_id],[city_name]) VALUES (26,SUBSTRING(@string, @start, @end - @start))
        
          --INSERT INTO @output (splitdata)  
         --VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)

  end



Responses

Nice

Yes, really goodone.

Could have been better.

Cool.

?

Leave your comment