Re: SQL Where LIKE - Range it!

From: "Gregory Wood" <gregw(at)com-stock(dot)com>
To: "Steagus" <steagus(at)S1PA3M2FIL4TE9Ryahoo(dot)com>
Cc: "PostgreSQL-General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: SQL Where LIKE - Range it!
Date: 2001-04-27 14:30:34
Message-ID: 00c201c0cf26$b5e2f230$7889ffcc@comstock.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

When you use the AND boolean operator, you are asking for records that match
BOTH boolean expressions. And I don't know many words that start with A
*and* F. :)

You want to use the OR operator:

SELECT * FROM table_name WHERE last_name LIKE 'A%' OR last_name LIKE 'F%'

> Can anyone provide some details or insights on how to accomplish this?

If you want a range, you'll have to use a regular expression (or a whole
bunch of LIKE expressions for every value in the range. A regular expression
version would be:

SELECT * FROM table_name WHERE last_name ~ '^[A-F]'

The tilde (~) tells it to match on a regular expression, the carat (^) tells
it to match the beginning of the string, the brackets match a single
character, and the A-F matches one letter in that range.

Hope this helps!

Greg

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joel Burton 2001-04-27 14:33:01 Re: I am now Linux and PostgreSQL user, have a question
Previous Message Jelle Ouwerkerk 2001-04-27 14:28:19 rotating log files