Re: SQL Where LIKE - Range it!

Поиск
Список
Период
Сортировка
От Gregory Wood
Тема Re: SQL Where LIKE - Range it!
Дата
Msg-id 00c201c0cf26$b5e2f230$7889ffcc@comstock.com
обсуждение исходный текст
Ответ на SQL Where LIKE - Range it!  (steagus@S1PA3M2FIL4TE9Ryahoo.com (Steagus))
Список 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 по дате отправления:

Предыдущее
От: larry a price
Дата:
Сообщение: Re: Re: Report Writer for PostgreSQL
Следующее
От: Peter Eisentraut
Дата:
Сообщение: Re: starting personal postmaster