0

I'm wondering what the heck is wrong with this?

CREATE OR REPLACE FUNCTION maxNumber(int4)
RETURNS int4 AS $$
DECLARE
  v_year ALIAS FOR $1; 
BEGIN
  RETURN (
    SELECT count(*)
    FROM table
    WHERE stringcol ~ '[0-9]{2,3}.[0-9]{1,3}.v_year.[0-9]{1,3}'
  );
END;
$$ LANGUAGE 'plpgsql' VOLATILE;

It always returns 0. The SQL itself seems to work. Thanks.

1
  • 1
    You should escape . as \\. Commented Dec 10, 2014 at 9:50

2 Answers 2

1
CREATE OR REPLACE FUNCTION maxNumber(int4)
RETURNS int4 AS $$
DECLARE
BEGIN
    RETURN (SELECT 
    coalesce(
      MAX(
        CAST(
          substring(stringcol, '[0-9]{1,3}$')
        AS int)
      )
    , 0)
    FROM table 
    WHERE stringcol ~ (E'\\' || '.' || to_char($1, 'FM09') || E'\\' || '.[0-9]{1,3}$'));
END;
$$ LANGUAGE 'plpgsql' VOLATILE;
Sign up to request clarification or add additional context in comments.

Comments

0

Shouldn't that be more like:

(E'[0-9]{2,3}\\.[0-9]{1,3}\\.' || v_year || E'\\.[0-9]{1,3}')

... as in, you're using a literal v_year as is, rather than concatenating its value with the two other sides of the expression.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.