How to find the first and last occurrences of a specific character inside a string in PostgreSQL

前端 未结 7 1157
闹比i
闹比i 2021-01-11 20:26

I want to find the first and the last occurrences of a specific character inside a string. As an example, consider a string named \"2010-####-3434\", and suppose the charact

7条回答
  •  囚心锁ツ
    2021-01-11 20:42

    In the case where char = '.', an escape is needed. So the function can be written:

    CREATE OR REPLACE FUNCTION last_post(text,char) 
    RETURNS integer LANGUAGE SQL AS $$  
    select length($1)- length(regexp_replace($1, E'.*\\' || $2,''));  
    $$;
    

提交回复
热议问题