March 19, 2011 at 12:37 pm
I track the URL referrers to my site
So I have data like
http://www.google.com/pages?=hh
http://www.google.com/vd/hhh/pages?=yy
http://www.Adomain.com/pages?=hh
http://www.Adomain.com/vd/hhh/pages?=yy
I am after http://www.thedomain.com and nothing else..easy to do this in client script, how does one do this in TSQL ??
March 19, 2011 at 3:20 pm
Try this:
DECLARE @URLTable TABLE
(
URLs VARCHAR(100)
)
INSERT INTO @URLTable
SELECT 'http://www.google.com/pages?=hh'
UNION ALL SELECT 'http://www.google.com'
UNION ALL SELECT 'http://www.google.com/vd/hhh/pages?=yy'
UNION ALL SELECT 'http://www.Adomain.com/pages?=hh'
UNION ALL SELECT 'http://www.Adomain.com'
UNION ALL SELECT 'http://www.Adomain.com/vd/hhh/pages?=yy'
SELECT SUBSTRING( URLs , INDEX_SLASH , (CHARINDEX('/', URLS + '/',INDEX_SLASH) - INDEX_SLASH)) URLs
FROM
( SELECT URLS ,(CHARINDEX('/',URLS) + 2) AS INDEX_SLASH FROM @URLTable ) INDEXED
March 19, 2011 at 3:36 pm
Here's a slightly different approach:
SELECT SUBSTRING(URLs,8,charindex('/',URLs+'/',8)-8) AS URLs
FROM @URLTable
March 19, 2011 at 3:39 pm
LutzM (3/19/2011)
Here's a slightly different approach:
SELECT SUBSTRING(URLs,8,charindex('/',URLs+'/',8)-8) AS URLs
FROM @URLTable
Smart Lutz!! I dint notice that the first occurance of the slash will always be at 8
Sloppy me
March 19, 2011 at 4:26 pm
Thanks:-)
March 19, 2011 at 7:34 pm
ColdCoffee (3/19/2011)
LutzM (3/19/2011)
Here's a slightly different approach:
SELECT SUBSTRING(URLs,8,charindex('/',URLs+'/',8)-8) AS URLs
FROM @URLTable
Smart Lutz!! I dint notice that the first occurance of the slash will always be at 8
Sloppy me
Is there any chance of having a https? If so, do a charindex('//',URLs)+2 to get the starting position.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
March 19, 2011 at 9:45 pm
Sorry wayne where do I put CHARINDEX('//',URLs)+2
Can you show me the full select thanks
March 20, 2011 at 6:51 am
Digs (3/19/2011)
Sorry wayne where do I put CHARINDEX('//',URLs)+2Can you show me the full select thanks
You would replace each of the "8"'s with that.
;with cte AS
(
SELECT URLs,
StartPos = CharIndex('//', URLs)+2
FROM @URLTable
)
SELECT SUBSTRING(URLs,StartPos,charindex('/',URLs+'/',StartPos)-StartPos) AS URLs
FROM cte;
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
March 21, 2011 at 2:13 pm
If you are doing this from a .net site you can use System.Uri server side before it hits sql to get what you need like this.
string myOutput = new Uri("http://www.sqlservercentral.com/Forums/Topic1080875-1291-1.aspx").Host;
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 9 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy