Ask the Expert

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?

    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: