Re: Pattern Matching - Range of Letters

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

William Garrison wrote:
> That won't work if you have a value "Anz" in there. It would be in
> the gap between An and Am.
Yes, I realized that too. My solution to it is a bit of a hack, but it's
easy and it works for me in this case. I translate everything to
uppercase and simply append 'ZZZZZZ' to the end of the second string.
None of the strings I am comparing to are longer than 6 characters, and
there are no numerical values in them.

Ron

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

Browse pgsql-general by date

  From Date Subject
Next Message Ottavio Campana 2007-05-10 23:19:48 Re: tokenize string for tsearch?
Previous Message garry saddington 2007-05-10 22:02:16 xml to db converter