Re: SQL Where LIKE - Range it!

From: Joel Burton <jburton(at)scw(dot)org>
To: Steagus <steagus(at)S1PA3M2FIL4TE9Ryahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL Where LIKE - Range it!
Date: 2001-04-27 14:37:41
Message-ID: Pine.LNX.4.21.0104271034220.17539-100000@olympus.scw.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 26 Apr 2001, Steagus wrote:

>
> What I'd like to do is pull a list of records where there is a range
> of last names; say from A - F.
> select * from table where last_name LIKE 'A%' AND last_name LIKE 'F%'
> - for example.
>
> The above code I've tried for this doesn't seem to work as I'd expect
> it too?
> I've even done
> select * from table where last_name LIKE 'A%' AND LIKE 'F%'
>
> Can anyone provide some details or insights on how to accomplish this?

LIKE A% AND LIKE F%

means "must start with A *AND* must start with F", so the name
"Anderson" would fail because it does start with A, but doesn't start with
F.

Something like

LIKE "A%" OR LIKE "B%" OR LIKE "C%" ... OR LIKE "F%"

would do the trick, but slowly, and it's a pain to write out.

I'd use

BETWEEN 'A' AND 'FZZZ'

(or, to be more precise, >='A' and <'G')

Keep in mind that PostgreSQL is case-sensitive, so if me name were
'Joel deBurton', you wouldn't find me. If you have lower-case starting
names, you'll want to see

(BETWEEN 'A' AND 'FZZZ') OR (BETWEEN 'a' AND 'fzzz')

HTH,
--
Joel Burton <jburton(at)scw(dot)org>
Director of Information Systems, Support Center of Washington

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joel Burton 2001-04-27 14:51:39 Re: running pgaccess on localhost
Previous Message Frank Bax 2001-04-27 14:35:37 Re: SQL Where LIKE - Range it!