raterus
asked on
Strip third+ level domains from a list
Hi, I'm trying to develop a query to consolidate and get just the 1st and 2nd level domains in a list.
e.g. this query will take
blah.somedomain.com
fourth.third.somedomain.co m
whatever.somedomain.com
test.anotherdomain.com
fourth.test.anotherdomain. com
and simply return
somedomain.com
anotherdomain.com
Can I do this from a sql query, if so how?
e.g. this query will take
blah.somedomain.com
fourth.third.somedomain.co
whatever.somedomain.com
test.anotherdomain.com
fourth.test.anotherdomain.
and simply return
somedomain.com
anotherdomain.com
Can I do this from a sql query, if so how?
ASKER
Hmm, close, but that assumes the domain only has a third/second/top syntax, so it turns "fourth.third.second.first " into "third.second.first", which isn't what I want.
Thanks for your help so far!
Thanks for your help so far!
ASKER
Got It!
select distinct reverse(substring(reverse(hostname), 0, charindex('.', reverse(hostname), charindex('.', REVERSE(hostname))+1)))
from MyTable
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for raterus's comment #a38459830
for the following reason:
This is the only solution that answers the question.
Accepted answer: 0 points for raterus's comment #a38459830
for the following reason:
This is the only solution that answers the question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Much cleaner, I would never had guess to use PARSENAME as a string function, but it works the same here.
PARSENAME() is a string function, it is just normally used for SQL Server Objects.
Word of caution: PARSENAME() supports only 4 parts so if you have this fifth.fourth.third.somedom ain.com, it will break down. So feel free if you want to withdraw the points.
Word of caution: PARSENAME() supports only 4 parts so if you have this fifth.fourth.third.somedom
Open in new window