Tuesday, November 26, 2013

Character index sql-server in loop , data in Table

declare

@bu varchar(300)

set

@bu=',0100000011,0100000050,0100000051,0100000026,'

create

table #tempmaintable( inpbu varchar(12))

DECLARE

@Str NVARCHAR(MAX)

SET

@Str = @bu

DECLARE

@Part NVARCHAR(MAX)

DECLARE

@IND INT

SET

@IND = CHARINDEX(',',@Str)

DECLARE

@EIND INT set @EIND = 0

WHILE

(@IND != LEN(@STR))

BEGIN

SET @EIND = ISNULL(((CHARINDEX(',', @Str, @IND + 1)) - @IND - 1), 0)

insert into #tempmaintable SELECT (SUBSTRING(@Str, (@IND + 1), @EIND))

SELECT @IND = ISNULL(CHARINDEX(',', @STR, @IND + 1), 0)

END

select

* from #tempmaintable

No comments: