Re: SQL Where LIKE - Range it! - Mailing list pgsql-general

From Gregory Wood
Subject Re: SQL Where LIKE - Range it!
Date
Msg-id 00c201c0cf26$b5e2f230$7889ffcc@comstock.com
Whole thread Raw
In response to SQL Where LIKE - Range it!  (steagus@S1PA3M2FIL4TE9Ryahoo.com (Steagus))
List 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


pgsql-general by date:

Previous
From: larry a price
Date:
Subject: Re: Re: Report Writer for PostgreSQL
Next
From: Peter Eisentraut
Date:
Subject: Re: starting personal postmaster