Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: Regular Expression Data Type


  • From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
  • To: Richard Doust <rdoust(at)mac(dot)com>
  • Cc: pgsql-general(at)postgresql(dot)org
  • Subject: Re: Regular Expression Data Type
  • Date: Sat, 21 Apr 2007 11:29:51 -0500
  • Message-id: <B42A8EFE-D695-4051-B778-E97B1C34BEA9(at)seespotcode(dot)net>


On Apr 21, 2007, at 11:01 , Richard Doust wrote:

select price from shipping_prices where shipFromZip = '23773' and shipToZip ~ '87927'

because shipToZip is defined as a regular expression, I'd match a row where shipToZip held the value '879[0-9]{2,2}' or '87[0-9]*'.

Wouldn't that be cool? Does anyone know whether this is already possible?

As the regex is just a text string, you could store regexen as text and just flip your regex expression around:

CREATE TABLE shipping_prices (
    shipping_price_id SERIAL PRIMARY KEY
    , shipping_price NUMERIC NOT NULL
    , ship_from_zip TEXT NOT NULL
    , ship_to_zip TEXT NOT NULL
    , UNIQUE (ship_from_zip, ship_to_zip));
NOTICE: CREATE TABLE will create implicit sequence "shipping_prices_shipping_price_id_seq" for serial column "shipping_prices.shipping_price_id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "shipping_prices_pkey" for table "shipping_prices" NOTICE: CREATE TABLE / UNIQUE will create implicit index "shipping_prices_ship_from_zip_key" for table "shipping_prices"
CREATE TABLE
INSERT INTO shipping_prices (shipping_price, ship_from_zip, ship_to_zip) VALUES (2.00,'23773', '879[0-9]{2,2}'); INSERT INTO shipping_prices (shipping_price, ship_from_zip, ship_to_zip) VALUES (3.00,'23775', '879[0-9]{2,2}'); INSERT INTO shipping_prices (shipping_price, ship_from_zip, ship_to_zip) VALUES (1.00,'23773', '877[0-9]{2,2}');
SELECT * FROM shipping_prices;
shipping_price_id | shipping_price | ship_from_zip |  ship_to_zip
-------------------+----------------+---------------+---------------
                1 |           2.00 | 23773         | 879[0-9]{2,2}
                2 |           3.00 | 23775         | 879[0-9]{2,2}
                3 |           1.00 | 23773         | 877[0-9]{2,2}
(3 rows)

SELECT shipping_price
FROM shipping_prices
WHERE ship_from_zip = '23773'
    AND '87927' ~ ship_to_zip;
shipping_price
----------------
          2.00
(1 row)

There may be another way, but I believe this should work, if I understand you correctly.

Michael Glaesemann
grzm seespotcode net





Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group