Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting programmatically 1

Status
Not open for further replies.

serializer

Programmer
May 15, 2006
143
SE
I have a table that consists of emails. As you know email consists of prefix@domain.extension. I wonder if it is possible to construct and sql thats sorts on the domain name and forward? For example:

xerxes@domainA.com
alpha@domainB.com

thanks!
 
Something like below should do the trick. Couple of notes: This assumes only ONE at-symbol (@), and This assumes you only want to sort based on the domain name as a whole.

Code:
DECLARE @Addresses AS TABLE
(
	EmailAddress			VARCHAR(100)
)

INSERT INTO @Addresses (EmailAddress) VALUES ('rjohnson-remove@ultimatemedical.edu')
INSERT INTO @Addresses (EmailAddress) VALUES ('rjohnson2-remove@ultimatemedical.edu')
INSERT INTO @Addresses (EmailAddress) VALUES ('rjohnson-remove@somewhere.com')
INSERT INTO @Addresses (EmailAddress) VALUES ('lhurgoyf-remove@tampabay.rr.com')
INSERT INTO @Addresses (EmailAddress) VALUES ('wildmage-remove@tampabay.rr.com')
INSERT INTO @Addresses (EmailAddress) VALUES ('robert.johnson-remove@tampabay.rr.com')

SELECT
	EmailAddress
	, SUBSTRING(EmailAddress, CHARINDEX('@', EmailAddress) + 1, 1000) 'DomainName'
FROM @Addresses
ORDER BY
	SUBSTRING(EmailAddress, CHARINDEX('@', EmailAddress) + 1, 1000)
	, EmailAddress

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top