1 Declare @ List Varchar ( 100 ), @ CNT Int , @ Idxstart Int , @ Idxend Int 2 Declare @ Sites Table (ID Int ) 3 4 Set @ List = ' 23, 34, 44, 56, 78 ' ; 5 -- Set @ list = '23 '; 6 7 Set @ List = Replace ( @ List , ' ' , '' ) 8 9 If ( Right ( @ List , 1 ) <> ' , ' ) 10 Set @ List = @ List + ' , ' 11 12 Set @ CNT = Len ( Replace (@ List , ' , ' , ' ,, ' )) - Len ( @ List ); 13 Set @ Idxstart = 0 14 Set @ Idxend = 0 15 16 While @ CNT > 0 17 Begin 18 Set @ Idxend = Charindex (' , ' , @ List , @ Idxstart ) 19 Insert Into @ Sites Values ( Convert ( Int , Substring ( @ List ,@ Idxstart , @ Idxend - @ Idxstart ))); 20 21 Set @ Idxstart = @ Idxend + 1 22 Set @ CNT = @ CNT - 1 23 End 24 25 Select * From @ Sites ;
In fact, it is very simple, it should be better understood. There are n many other implementations in the garden, you can look at this (T-SQL split) feel good. The key is the idea.