Re: String manipulation - Mailing list pgsql-general

From pativo@arcor.de (pativo)
Subject Re: String manipulation
Date
Msg-id 58babf1b.0402180117.31e1cc91@posting.google.com
Whole thread Raw
In response to String manipulation  (pativo@arcor.de (pativo))
List pgsql-general
pativo@arcor.de (pativo) wrote in message news:<58babf1b.0402170346.4a719868@posting.google.com>...

Hello and thanks to all!

I'm amused that I get so many responses.

Ok, at time I use a plpgsql function (isAHex(VARCHAR)) which checks only
if the length is even.
So I think I should implement the following.

=======8<-------------------------------------------------------------------

CREATE FUNCTION isAHex(VARCHAR) RETURNS BOOLEAN AS '
   DECLARE
      input_text_p ALIAS FOR $1;
      tmp_char VARCHAR;
   BEGIN
      IF ((length(input_text_p) % 2) <> 0) THEN
         RETURN FALSE;
      END IF;
      FOR i IN 1..length(input_text_p) LOOP
         tmp_char := substr(input_text_p, i, 1);
         IF NOT tmp_char ~ ''[0-9a-fA-F]'' THEN
            RETURN FALSE;
         END IF;
      END LOOP;
      RETURN TRUE;
   END;
' LANGUAGE 'plpgsql';

CREATE TABLE textsTest (
   surrogate   VARCHAR(40)   PRIMARY KEY,
   hex_text    VARCHAR(150)  NOT NULL CHECK(isAHexTest(hex_text))
);

=======8<-------------------------------------------------------------------


But for next release I will use the following. This is in my eyes
the better solution.

=======8<-------------------------------------------------------------------

CREATE TABLE textsTest (
   surrogate   VARCHAR(40)   PRIMARY KEY,
   hex_text    TEXT          NOT NULL CHECK(text ~ '^([0-9A-Fa-f]{2})+$')
);

=======8<-------------------------------------------------------------------


Thanks to all!!!

oki,


pativo

--
www.pativo.de

pgsql-general by date:

Previous
From: Linh Luong
Date:
Subject: finding out actual tuple size
Next
From: Andrew Sullivan
Date:
Subject: Re: finding out actual tuple size