Question

I'm using SQL Server 2012.

I want to query data from a specific SQL column that meets certain criteria. This column contains free form text entered by a user. The user can enter whatever he/she wants, but always includes a URL which may be entered anywhere within the free form text.

Each URL is similar and contains consistent elements, such as the domain, but also references a unique "article ID" number within the URL. Think of these numbers as referencing knowledge base articles.

The article ID is a different number depending on the article used and new articles are regularly created.

I need a query identifying all of these article ID numbers within the URLs. The only means I've developed so far is to use SUBSTRING to count characters until reaching the article ID number. This is unreliable since users don't always include the URL at the beginning. It would be better if I could tell SUBSTRING to count from the beginning of the URL regardless of where it resides within the text.

For example, it begins counting whenever it finds 'HTTP://' or a common keyword each URL contains. Another option would be if I could extract the URL into it's own table. I've yet to figure out how to execute either of these ideas inside SQL.

The following is what I have so far.

select 
    scl.number, 
    ol.accountnum, 
    scl.opendate as CallOpenDate, 
    sce.opendate as NoteEntryDate, 
    sce.notes, 
    substring(sce.notes, 102, 4) as ArticleID, 
    sclcc.pmsoft, 
    ol.territorydesc

from (select * from supportcallevent as sce
    where sce.opendate > '2014-04-01 00:00:00.000') as sce

inner join supportcalllist as scl on scl.SupportCallID=sce.supportcallid
inner join organizationlist as ol on ol.partyid=scl.partyid
inner join supportcalllist_custcare as sclcc on sclcc.supportcallid=scl.supportcallid

where sce.notes like '%http://askus.how%'

order by ol.territorydesc, scl.number;
Was it helpful?

Solution

You can use the CHARINDEX function to find the URL in the string and start the substring from there.

This example will get the next 4 digits after the url:

DECLARE @str VARCHAR(100)
DECLARE @find VARCHAR(100)

SET @str = 'waawhbu aoffawh http://askus.how/1111 auwhauowd'
SET @find = 'http://askus.how/'

SELECT SUBSTRING(@str,CHARINDEX(@find, @str)+17,4) 

SQLFiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top