pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: Order by email address
2+ messages / 1 participants
[nested] [flat]

* Re: Order by email address
@ 2001-05-10 20:17  Josh Berkus <josh@agliodbs.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Josh Berkus @ 2001-05-10 20:17 UTC (permalink / raw)
  To: pgsql-sql

Herve,

> toto@aol.com
> tutu@aol.com
> toto@be.com
> tutu@be.com
> toto@yahoo.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@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Order by email address
@ 2001-05-10 20:38  Josh Berkus <josh@agliodbs.com>
  parent: Josh Berkus <josh@agliodbs.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Josh Berkus @ 2001-05-10 20:38 UTC (permalink / raw)
  To: pgsql-sql

Herve, Etc.

Ooops!  A couple of misteaks in that last post:

> > toto@aol.com
> > tutu@aol.com
> > toto@be.com
> > tutu@be.com
> > toto@yahoo.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

I meant:
	ORDER BY domain, mailbox

> 
> 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;

I meant:
	RETURN mail_domain || mail_box;

> END;'
> LANGUAGE 'plpgsql';
> 
> Then:
> 
> SELECT user_id, email, email_sort(email) as sortcol
> FROM users
> ORDER BY sortcol;
> 

-Josh

______AGLIO DATABASE SOLUTIONS___________________________
                                       Josh Berkus
  Complete information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2001-05-10 20:38 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-05-10 20:17 Re: Order by email address Josh Berkus <josh@agliodbs.com>
2001-05-10 20:38 ` Josh Berkus <josh@agliodbs.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox