Re: How to search ignoring spaces and minus signs

From: Christian Ramseyer <rc(at)networkz(dot)ch>
To: Andrus <kobruleht2(at)hot(dot)ee>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to search ignoring spaces and minus signs
Date: 2010-10-14 01:24:12
Message-ID: 4CB65BBC.1020802@networkz.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10/13/2010 07:45 PM, Andrus wrote:
> CHAR(20) columns in 8.4 database may contains spaces and - signs like
>
> 13-333-333
> 12 3-44
> 33 33 333
> 12345
>
> User enters code to search without spaces and - signs, like 12344
> How to search for product code ignoring spaces and - signs?
>

There are many options to do that...

You could use a regular expression like this:

DMP=# select * from foo where prod_code ~ '1[ -]*2[ -]*3[ -]*4[ -]*4';
prod_code
-----------
12 3-44
12-3-44
123 44

[ -]* means "zero or more dashes or spaces".

Maybe easier or (computationally) faster (YMMV on both counts) would be
to replace() the dashes and spaces on the fly first and only search the
cleaned string:

DMP=# select prod_code,
replace(replace(prod_code, '-', ''),' ','') from foo
where replace(replace(prod_code, '-', ''),' ','') = '12344';
prod_code | replace
-----------+---------
12 3-44 | 12344
12-3-44 | 12344
123 44 | 12344

Or just store the codes in a uniform format to begin with.

Christian

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Grzegorz Jaśkiewicz 2010-10-14 01:35:15 Re: How to search ignoring spaces and minus signs
Previous Message Josh Kupershmidt 2010-10-13 22:37:52 Re: How to search ignoring spaces and minus signs