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

前端 未结 7 1164
闹比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 21:01

    Well...

    Select position('#' in '2010-####-3434');
    

    will give you the first. If you want the last, just run that again with the reverse of your string. A pl/pgsql string reverse can be found here.

    Select length('2010-####-3434') - position('#' in reverse_string('2010-####-3434')) + 1;
    
    0 讨论(0)
提交回复
热议问题