# While loop use charindex

**URL:** <https://forums.thehomestack.io/t/while-loop-use-charindex/315>\
**Category:** Databases\
**Created:** [October 12, 2008, 5:09pm UTC](https://forums.thehomestack.io/t/while-loop-use-charindex/315 "2008-10-12T17:09:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://forums.thehomestack.io/uploads/default/original/1X/161426793bc0d8bdf0d1a6692b3e44446861cc72.jpeg) [@system](https://forums.thehomestack.io/u/system)\
**Post date:** [October 12, 2008, 5:09pm UTC](https://forums.thehomestack.io/t/while-loop-use-charindex/315/1 "2008-10-12T17:09:42Z")

</div>

Here is an example of a loop using charindex

```auto
SELECT statement.

declare @site as varchar(20)
set @site = ‘1,2,3,4,5,6,7,8,9′

declare @t1 table
(
t1 int
)

while charindex(’,',@site) > 0
begin
insert into @t1 select substring(@site,1,1)
SET @site = substring(@site,3,len(@site))
end

Select * from table1
where site in ( select * from @t1)

```
