drop table computers
create table Computers (computername varchar(20), OU1 varchar(20), OU2 varchar(20))
declare @rawldapstring varchar(255)
declare @str varchar(255)
declare @commapos int
declare @computername varchar(20)
declare @OU1 varchar(20)
declare @OU2 varchar(20)
declare @counter int
set @counter=0
set @rawldapstring=(select min([raw ldap string]) from test)
while @counter<(select count (*) from test)
begin
set @str=@rawldapstring
--find position of first comma
set @commapos=charindex(',',@str)
--start after 11 characters and end at comma
set @computername=(substring(@str,11,@commapos-11))
--redefine string as what remains
set @str =replace(@str,'LDAP://CN=' + @computername +',','')
--do roughly same process twice more
set @commapos=charindex(',',@str)
set @OU1=substring(@str,4,@commapos-4)
set @str =replace(@str,'OU=' + @OU1 +',','')
set @commapos=charindex(',',@str)
set @OU2=substring(@str,4,@commapos-4)
insert into Computers values (@computername, @OU1, @OU2)
set @counter=@counter+1
set @rawldapstring=(select min([raw ldap string]) from test where [raw ldap string]>@rawldapstring)
end
select * from computers
|