In SQL Server I execute:
select PATINDEX('%\%[0123456789][\ ]%', N'\deftab1134\paperw12240\paperh20000\margl900\margt1440\margr540\margb1440\plain\f1\fs24 That is my report');
---
1
It is correct. I need the same function in PostgreSQL. I have found this function:
CREATE OR REPLACE FUNCTION patindex( "pattern" TEXT, "expression" TEXT)
RETURNS INT
AS $BODY$
SELECT
COALESCE(
STRPOS(
lower($2)
,(
SELECT
lower(( REGEXP_MATCHES(
lower($2)
,'(' || REPLACE( REPLACE( lower(TRIM( $1, '%' )), '%', '.*?' ), '_', '.' ) || ')'
) )[ 1 ])
LIMIT 1
)
)
,0
)
;
$BODY$ LANGUAGE 'sql' IMMUTABLE;
But it works incorrectly with the same parameters:
select helper2_patindex('%\%[0123456789][\ ]%',
'\deftab1134\paperw12240\paperh20000\margl900\margt1440\margr540\margb1440\plain\f1\fs24 That is my report');
----
87
What is incorrect? what can I fix?