Q

Sorting an E-mail column on Domain name

I have a 20,000+ Goldmine Database. I would like to sort the database by e-mail addresses. Not by what is before the "@" symbol. I want to sort by what's after the @symbol, i.e., @foo.com, @foobar.net... Can you give me a clue?

Here's the approach, illustrated with standard SQL. Use the POSITION string function to find the "@" in the e-mail address, and the SUBSTRING and LENGTH functions to pull out the rest of the string:

select emailaddress
     , substring (
         emailaddress
       from position('@' in emailaddress) + 1
       for length(emailaddress)
             - position('@' in emailaddress)
                 ) as domainname
  from goldminetable
order
    by domainname

I'm not familiar with the Goldmine database, so maybe it has its own non-standard syntax. For example, in Microsoft Access you have to say:

select emailaddress
     , mid (
         emailaddress
       , instr(emailaddress,'@') + 1
       , len(emailaddress)
             - instr(emailaddress,'@')
                 ) as domainname
  from goldminetable
order
    by mid (
         emailaddress
       , instr(emailaddress,'@') + 1
       , len(emailaddress)
             - instr(emailaddress,'@')
                 )

Some databases may not let you use the column alias "domainname" in the ORDER BY clause, so just repeat the function expression.

For More Information


This was first published in November 2002

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

1 comment

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close