简体   繁体   中英

Select query to return all rows where a column contains a domain name

I have a column that stores URLs and I would like to generate a select query to pull back all URLs containing particular domain name(s).

Example of URLs stored: http://www.fox.com/files/apple.jpg , http://mail.redfox.com/help/index.aspx , http://apple.com/ etc.

I know what you're already thinking, just use LIKE.

The reason I can't is: SELECT * WHERE Domains IS LIKE %fox.com% it will return http://www.fox.com/files/apple.jpg along with http://mail.redfox.com/help/index.aspx (since they both contain fox.com).

I'm not all the savvy with SQL other than your basic queries, SQL Server can do some sort of regex magic I'm unfamiliar with?

You could test the domain name for both subdomains and naked domain:

WHERE Domains IS LIKE '%.fox.com/%' OR Domain IS LIKE '%/fox.com/%'

Sure, you can do it with a REGEX ( [\\/\\.]fox.com\\/ ), but simple SQL might be easier to remember/understand later on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM