Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 all strings between...

Author  Topic 

tleforge
Starting Member

3 Posts

Posted - 2005-05-09 : 17:01:40
I would like to select a record and then from one of the columns, return all strings between [some identifier] and [another identifier].

Specifically, I'm looking to return all strings that exist between

'<a' and the next '/a>'

I've tried using regular expressions, but can't seem to wrap my brain around it. Especially when multiple values might exist within a record/column.

For instance, it's possible that a record might contain the following:

... please follow <a href="http://www.hello.com">this link</a> or <a href="somepage.asp">this link</a>.

I would like to return '<a href="http://www.hello.com">this link</a>, <a href="somepage.asp">this link</a>'.

Thank you VERY much for any assistance.

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-05-09 : 17:10:53
this should help you understand:

declare @string varchar(100),
@start int,
@end int

set @string = 'please follow <a href="http://www.hello.com">this link</a> or <a href="somepage.asp">this link</a>'

-----------------------------------------------
-- find beginning position of the '<' in '<a'
-----------------------------------------------
select @start = charindex('<a', @string)

-----------------------------------------------
-- find ending position of the '>' in '</a>'
-----------------------------------------------
select @end = charindex('</a>',@string) + len('</a>')

-----------------------------------------------
-- use substring func to extract lenghth between
-----------------------------------------------
select substring(@string, @start, (@end - @start))
Go to Top of Page

tleforge
Starting Member

3 Posts

Posted - 2005-05-10 : 09:15:02
nathans,
Thank you very much for your reply. One question...

When using your code, I take it I would need to put it in a loop until charindex gave me a 0 (maybe negative 1, I can look that up) or the index was the same as the len of the original string. Is that about right?

Thanks again.

Tim L
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-05-10 : 12:39:45
a loop solution would be something like...


declare @string varchar(1000),
@start int,
@end int,
@url varchar(8000)

set @string = 'please follow <a href="http://www.hello.com">this link</a> or <a href="somepage.asp">this link</a> or <a href="onemore.asp"</a>'

select @start = charindex('<a', @string),
@end = charindex('</a>',@string) + len('</a>'),
@url = ''

while @start <> 0
begin
-- print @string

-- extract loop, comma separate if not blank
select @url = case
when @url <> '' then @url + ', ' + substring(@string, @start, @end - @start)
else @url + substring(@string, @start, (charindex('</a>',@string) + len('</a>') - @start))
end
-- remove extracted url from @string
select @string = stuff(@string, @start, @end - @start, '')


-- support loop
select @start = charindex('<a', @string),
@end = charindex('</a>',@string) + len('</a>')

end
select @url
Go to Top of Page

tleforge
Starting Member

3 Posts

Posted - 2005-05-10 : 12:43:45
nathans,

Perfect. You're awesome! Thank you SO much.


Tim L
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-05-10 : 14:06:07
glad to help :)

Nathan Skerl
Go to Top of Page
   

- Advertisement -