Re: ORDER BY case insensitive?

From: Jason Earl <jdearl(at)yahoo(dot)com>
To: Bob Swerdlow <swerdlow(at)maine(dot)rr(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: ORDER BY case insensitive?
Date: 2001-10-04 21:22:55
Message-ID: 20011004212255.3654.qmail@web10002.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

My guess is that compared to the task of sorting
millions of names the fact that you have to lowercase
them first is not going to be a particular burden. No
matter what you do you are going to get a table scan
(unless you qualify your select with a where clause).

You can, however, create an index like:

create index MyTable_lower_idx on MyTable
(lower(name));

It won't help with your particular query, but it
certainly would help for queries like:

SELECT * FROM MyTable WHERE lower(name) = 'jason';

It is also possible to create a trigger that would
automatically lowercase information when it is
inserted or updated. Or even less high tech you can
make sure that all of your INSERTS and UPDATES use the
lower command like this:

INSERT into MyTable (name) VALUES (lower('VALUE'));

Jason

--- Bob Swerdlow <swerdlow(at)maine(dot)rr(dot)com> wrote:
> Thanks for the suggestion, Jason.
>
> A co-worker of mine, however, had this response:
> Yes, but my guess is that that will first
> convert all million (or
> whatever) records to upper case, and then physically
> sort them. It won't be
> able to make use of the index.
>
> To make this efficient, do we need to uppercase all
> of the data before
> adding to the table? (yuk)
>
> - Bob
>
>
> ----- Original Message -----
> From: "Jason Earl" <jdearl(at)yahoo(dot)com>
> To: "Bob Swerdlow" <swerdlow(at)maine(dot)rr(dot)com>;
> <pgsql-sql(at)postgresql(dot)org>
> Sent: Thursday, October 04, 2001 3:47 PM
> Subject: Re: [SQL] ORDER BY case insensitive?
>
>
> > SELECT * FROM MyTable ORDER BY lower(Name);
> >
> > Should do the trick.
> >
> > Jason Earl
> >
> > --- Bob Swerdlow <swerdlow(at)maine(dot)rr(dot)com> wrote:
> > > How do I get the rows sorted in a case
> insensitive
> > > way?
> > >
> > > I have some queries that basically fit the form:
> > > SELECT * FROM MyTable ORDER BY Name;
> > > When I view the results, all of the Name's that
> > > start with an upper case
> > > letter precede all of the Name's that start with
> a
> > > lower case letter. I
> > > want them all in alphabetic order, regardless of
> > > case.
> > >
> > > I've looked in PostgreSQL Introduction and
> Concepts
> > > by Bruce Momjian and in
> > > the FAQ at
> > >
> http://postgresql.overtone.org/docs/faq-english.html
> > >
> > > Thanks for your help.
> > >
> > > --
> > > Bob Swerdlow
> > > Chief Operating Officer
> > > Transpose, LLC
> > > rswerdlow(at)transpose(dot)com
> > >
> > >
> > >
> > > ---------------------------(end of
> > > broadcast)---------------------------
> > > TIP 6: Have you searched our list archives?
> > >
> > > http://archives.postgresql.org
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > NEW from Yahoo! GeoCities - quick and easy web
> site hosting, just
> $8.95/month.
> > http://geocities.yahoo.com/ps/info1
> >
>

__________________________________________________
Do You Yahoo!?
NEW from Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
http://geocities.yahoo.com/ps/info1

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2001-10-04 22:20:20 Re: ORDER BY case insensitive?
Previous Message Marat Khairullin 2001-10-04 21:02:36 Why the weak key is created as unique