0

I'm trying to show in SQL only the URL that have a path, for example:

stackoverflow.com/ - no path

stackoverflow.com/abc.html - no path

stackoverflow.com/abc- path

stackoverflow.com/abc/bla/- path

now in regex the answer is \/\w+$

I know regex is not working well with SQL, so i don't know how could i check URL.

this is what i tried:

select * from sites where url like '\/\w+$'
4
  • 1
    What kind database did you use, MySQL,MSSQL,oracle? Commented Feb 11, 2018 at 10:17
  • SQL server 2014 Commented Feb 11, 2018 at 10:18
  • 1
    Afaik the like operator uses Wildcards, not Regex Commented Feb 11, 2018 at 10:19
  • Your assumptions are wrong; stackoverflow.com/abc is not a path as abc is a file. Commented Feb 11, 2018 at 11:42

2 Answers 2

1

This checks if URL contains two slashes:

SELECT *
FROM testdata
WHERE url LIKE '%/%/%'

Technically speaking, the URLs stackoverflow.com/abc.html and stackoverflow.com/abc are identical, where you have a hostname and a filename. The above example will only match stackoverflow.com/abc/bla/.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use CHARINDEX:

WITH cte(p) AS (
    SELECT 'stackoverflow.com/' 
    UNION SELECT 'stackoverflow.com/abc.html' 
    UNION SELECT 'stackoverflow.com/abc'
    UNION SELECT 'stackoverflow.com/abc/bla'
)
SELECT *
FROM cte
WHERE CHARINDEX('/', REVERSE(p)) < CHARINDEX('.', REVERSE(p));

Rextester Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.