Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys
I need help.. really! Im running SQLserver 2016.

I have a table containing a lot og URLs which i would like to SELECT and trim to another colume having only the path.

The columne is called REDIRTO and the table is called EVENTLOG and values could be like:

mydomain.com/ordering
mydomain.com/shop/cart?23456
mydomain.com/profilepage/126378
us.mydomain.com/extranet
uk.mydomain.com/ordering

What i would like is a colume with the first value after the domain:

ordering
shop
profilepage
extranet
ordering


I have tried to use LEN bit it is not really working. Basically i want the string to be trimmed from left to the first / and to the next /

What I have tried:

SQL
Select EVENT_ID, REDIRTO, Right([REDIRTO],LEN([REDIRTO])-CharIndex('/',[REDIRTO])) as PATH
From EVENTLOG
Posted
Updated 1-Mar-18 21:50pm
v2
Comments
RedDk 1-Mar-18 14:03pm    
Just a warning ... and noone knows how complete your listing such&such really is, which is really the sticking point to question, but to do what you want the way you've done it in your example is fine. In conjunction with any of the numerous string/character functions. REGEX is another consideration. Perhaps you know that special characters were put on earth to roam the ridges freely and sack whatnot willy-nilly ... so basically your life will be a living hell.

As you are using SQL Server 2016 you can use the STRING_SPLIT[^] function to separate the parts of the text on the '/' characters e.g.
SQL
DECLARE @sample varchar(255) = 'mydomain.com/shop/cart?23456'
declare @temp table (id int identity(1,1), part varchar(255))
insert into @temp SELECT value FROM STRING_SPLIT(@sample, '/')
select part from @Temp where id = 2
If you can't use STRING_SPLIT then there are other examples of UDFs for splitting strings e.g. How to Split a string by delimited char in SQL Server.............. - SQLServerCentral[^]
Alternatively, you can use SUB_STRING with CHARINDEX ...it's a bit ugly though...
SQL
DECLARE @sample varchar(255) = 'mydomain.com/shop/cart?23456'
SELECT LEFT(SUBSTRING(@sample, CHARINDEX('/',@sample) + 1, LEN(@sample)), CHARINDEX('/', SUBSTRING(@sample, CHARINDEX('/',@sample) + 1, LEN(@sample)))-1)
 
Share this answer
 
Comments
Maciej Los 2-Mar-18 3:50am    
5ed!
I'd prefer to use Common Table Expressions[^]. See:

DECLARE @tmp TABLE(uriaddress varchar(255))

INSERT INTO @tmp (uriaddress)
VALUES('mydomain.com/ordering'),
('mydomain.com/shop/cart?23456'),
('mydomain.com/profilepage/126378'),
('us.mydomain.com/extranet'),
('uk.mydomain.com/ordering')

;WITH CTE AS 
(
	SELECT 1 AS PartNo, LEFT(uriaddress, CHARINDEX('/', uriaddress)-1) AS UriPart, RIGHT(uriaddress, LEN(uriaddress) - CHARINDEX('/', uriaddress)) AS Remainder
	FROM @tmp
	WHERE CHARINDEX('/', uriaddress)>0
	UNION ALL
	SELECT PartNo + 1 AS PartNo, LEFT(Remainder, CHARINDEX('/', Remainder)-1) AS UriPart, RIGHT(Remainder, LEN(Remainder) - CHARINDEX('/', Remainder)) AS Remainder
	FROM CTE
	WHERE CHARINDEX('/', Remainder)>0
	UNION ALL
	SELECT PartNo + 1 AS PartNo, Remainder AS UriPart, NULL AS Remainder
	FROM CTE
	WHERE CHARINDEX('/', Remainder)=0
)
SELECT * --UriPart
FROM CTE 
WHERE PartNo =2

Result:

PartNo	UriPart	Remainder
2	ordering	NULL
2	extranet	NULL
2	profilepage	126378
2	shop	    cart?23456
2	ordering	NULL


For further information about CTE, please see:
WITH common_table_expression (Transact-SQL) | Microsoft Docs[^]
Recursive Queries Using Common Table Expressions[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900