Re: How to search ignoring spaces and minus signs

From: Darren Duncan <darren(at)darrenduncan(dot)net>
To: Andrus <kobruleht2(at)hot(dot)ee>, pgsql-general(at)postgresql(dot)org
Subject: Re: How to search ignoring spaces and minus signs
Date: 2010-10-13 21:01:16
Message-ID: 4CB61E1C.8040808@darrenduncan.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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?
>
> For example searching for code 12344 should return
> 12 3-44 as matching item.

Is your search an exact match ignoring the spaces and dashes, or a substring
search? If the field contained 0123445 then is a search for 12344 supposed to
match it or not?

The best simple way to do what you want is to add another CHAR(20) column for
each of the existing ones like you describe where the extra column has a copy of
the original one but with the spaces and dashes removed.

Then when doing searches you search on the new copy and when displaying you
display the original copy.

Doing this would save the database having to do the most expensive kinds of
computations repeatedly at the time of searching given that these can be
staticly precomputed.

Moreover, if your search is exact-match, you get additional speed gains by
having an index on the search column. (I don't know if there is any kind of
useful index for substring matches besides full text search.)

-- Darren Duncan

In response to

Browse pgsql-general by date

  From Date Subject
Next Message ljb 2010-10-13 21:03:43 Re: Gripe: bytea_output default => data corruption
Previous Message Merlin Moncure 2010-10-13 20:58:54 Re: How to search ignoring spaces and minus signs