Re: Order by email address

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Order by email address
Date: 2001-05-10 20:17:49
Message-ID: web-54242@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Herve,

> toto(at)aol(dot)com
> tutu(at)aol(dot)com
> toto(at)be(dot)com
> tutu(at)be(dot)com
> toto(at)yahoo(dot)com
>
> Is it possible and how ?

Given the relational rule of Atomicity (each discrete piece of
information shall have its own column or row), the solution is for you
to make "e-mail id" and "domain" seperate fields. Then you can sort:

ORDER BY mailbox, domain

If this is a legacy database, and splitting the field is not an option
for you due to exisiting applications/policy, then you'll need to write
a custom sorting function:

CREATE FUNCTION email_sort (VARCHAR)
RETURNS CHAR(120) AS '
DECLARE
email_addr ALIAS for $1;
mail_box CHAR(60);
mail_domain CHAR(60);
BEGIN
mail_box := CAST(SUBSTR(email_addr, 1, (STRPOS(email_addr, ''@'', 1)
-1)) AS CHAR(60));
mail_domain := CAST(SUBSTR(email_addr, (STRPOS(email_addr, ''@'', 1) +
1), 60) AS CHAR(60));
RETURN mail_box || mail_domain;
END;'
LANGUAGE 'plpgsql';

Then:

SELECT user_id, email, email_sort(email) as sortcol
FROM users
ORDER BY sortcol;

However, this solution has a number of problems for data integrity down
the line. If e-mail addresses are that important to your application, I
greatly encourage you to split the field.

-Josh Berkus

P.S. Roberto, please add the above to our function library.
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2001-05-10 20:38:21 Re: Order by email address
Previous Message Bruce Momjian 2001-05-10 19:47:57 Re: Order by email address by domain ?