Search Postgresql Archives

Re: Regular Expression Data Type

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




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




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux