I am trying to split URL and get each part as domain
, category
, subcategory
etc and insert each part into a table. For example:
"www.mydomain.com/toolsanddownloads/dailymealplanner.html?languageid=6"
The purpose is to do 404 redirect if page don't exist. I am tryomg to write SQL statement usinng CTE and get each part of the domain
;with cte AS
(
SELECT
CASE
WHEN RIGHT(RTRIM(URL),1) = '/' THEN LEFT(URL,LEN(URL)-1)
WHEN RIGHT(RTRIM(URL),5) = '.html' THEN LEFT(URL,LEN(URL)-5)
ELSE URL
END AS URL1,
StartPos = CharIndex('//', URL)+2
FROM [dbo].[404RedirectTemp]
)
SELECT URL1, SUBSTRING(URL1, 8, CHARINDEX('/', URL1, 9) - 8) AS DomainName,
REVERSE(SUBSTRING(REVERSE(URL1), CHARINDEX('?', REVERSE(URL1)) + 1,
CHARINDEX('/', REVERSE(URL1)) - CHARINDEX('?', REVERSE(URL1)) -1)) AS CategoryName,
SUBSTRING(URL1, CHARINDEX('?', URL1) + 1, LEN(URL1)) AS QueryParameter
FROM cte;
I an getting always the last bit for category name and is wrong because some URL's are http://www.mydomain.com/toolsanddownloads/dailymealplanner.html?languageid=6
some
"www.mydomain.com/toolsanddownloads"
"www.mydomain.com/toolsanddownloads/dailymealplanner.html"
What i want to achieve is is no matter how many sections URL has I want to get them all as columns: domain
, categories
, subcategories
, brand
, product
If domain has only categories to get categories, if categories and subcategories to get subcategories
i have over 4000 URL in temp table which i want to loop through each one and update other table for 404 redirect