Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
You should escape . as \\. –  vks 17 hours ago

2 Answers 2

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.

share|improve this answer
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;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.