PatIndex Postgresql

As you may already notice postgresql does not contain the function patidex, it is for this reason that we bring them to you next:

CREATE OR REPLACE FUNCTION patindex(pattern VARCHAR, expression VARCHAR ) RETURNS INT AS $BODY$
SELECT
    COALESCE(
        STRPOS(
             $2
            ,(
                SELECT
                    ( REGEXP_MATCHES(
                        $2
                        ,'(' || REPLACE( REPLACE( TRIM( $1, '%' ), '%', '.*?' ), '_', '.' ) || ')'
                    ) )[ 1 ]
                LIMIT 1
            )
        )
        ,0
    )
;
$BODY$ LANGUAGE 'sql' IMMUTABLE;

We hope its usefulness

También te podría gustar...

Deja una respuesta

Tu dirección de correo electrónico no será publicada.