Re: Pattern Matching - Range of Letters

From: William Garrison <postgres(at)mobydisk(dot)com>
To: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
Cc: Ron St-Pierre <ron(dot)pgsql(at)shaw(dot)ca>, pgsql-general(at)postgresql(dot)org
Subject: Re: Pattern Matching - Range of Letters
Date: 2007-05-10 22:00:23
Message-ID: 464395F7.3090402@mobydisk.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

That won't work if you have a value "Anz" in there. It would be in the
gap between An and Am.

create table test (test text);
insert into test values ('A');
insert into test values ('b');
insert into test values ('c');
insert into test values ('d');
insert into test values ('e');
insert into test values ('Ab');
insert into test values ('Ac');
insert into test values ('Amz');
insert into test values ('Az');

select * from test where test between 'A' and 'Am';
"A"
"Ab"
"Ac"

select * from test where test between 'An' and 'Bc';
"Az"

I wouldn't use between in this case. I'd suggest this:
select * from test where test >= 'A' and test <'Am';
"A"
"Ab"
"Ac"

select * from test where test >= 'Am' and test <'Bc';
"Amz"
"Az"

The end will be tricky because ""zzzz is not < "zz" so you will need the
last select to be

select * from test where test >= 'Yi';

The beginning will be tricky too if you allow things that come before A
such as 0-9 or spaces.

Richard Broersma Jr wrote:
> --- Ron St-Pierre <ron(dot)pgsql(at)shaw(dot)ca> wrote:
>
>> I'm sure that others have solved this but I can't find anything with my
>> (google and archive) searches. I need to retrieve data where the text
>> field is within a certain range e.g.
>> A-An
>> Am-Bc
>> Bc-Eg
>> ....
>> Yi-Zz
>>
>> Does anyone know of a good approach to achieve this? Should I be looking
>> into regular expressions, or maybe converting them to their ascii value
>> first?
>
> Regular expressions would work, but a between statement should work also.
>
> SELECT *
> FROM Your_table AS YT
> WHERE YT.text_field BETWEEN 'Aa' AND 'An';
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message garry saddington 2007-05-10 22:02:16 xml to db converter
Previous Message novnov 2007-05-10 21:48:24 Windows installation; why is PL/python shown but disabled?