Re: String manipulation - Mailing list pgsql-general

From Pascal Polleunus
Subject Re: String manipulation
Date
Msg-id 403271CA.7080705@beeznest.net
Whole thread Raw
In response to String manipulation  (pativo@arcor.de (pativo))
Responses Re: String manipulation  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-general
You should be able to do that with a regular expression.

CHECK (text ~ '^([0-9A-F]{2})+$')

Remark: As the column is NOT NULL, I suppose that an empty string is not
valid. If an empty string must be valid, replace the + with * ;-)


pativo wrote:
> Hello to all,
>
> I have small problem. I have some database fields (VARCHAR)
> and these field should hold some ahex coded values. So the
> string length must be even and each character can only be
> 0-9, a-f or A-F.
> My idea was that:
>
> ====8<-----------------------------------
>
> CREATE TABLE test (
>     id   INT4         PRIMARY KEY NOT NULL DEFAULT nextID(),
>     text VARCHAR(150) NOT NULL CHECK(isAHex(text))
> );
>
> CREATE FUNCTION isAHex(VARCHAR) RETURNS BOOLEAN AS '
>    DECLARE
>       text_p ALIAS FOR $1;
>    BEGIN
>       IF ((length(text_p) % 2) <> 0) THEN
>          RETURN FALSE;
>       END IF;
>       -- TODO How to check each character
>       RETURN TRUE;
>    END;
> ' LANGUAGE 'plpgsql';
>
> ====8<-----------------------------------
>
> Has anybody an idea how could I check each character?
> I would prefer a solution in plpgsql!!
>
> Thank!
>
>
> pativo
>



pgsql-general by date:

Previous
From: Anton Nikiforov
Date:
Subject: Select statment question
Next
From: "Oleg Lebedev"
Date:
Subject: Re: Select statment question