StringUtil indexOf() 等效的 postgreSQL 查询

Sri*_*Sri 5 java postgresql indexof string-utils

我需要在 postgresql 中实现 stringUtils Class indexOf() 方法。

假设我有一个table其中url的列之一。

url : "http://paypal-info.com/home.webapps.cgi-bin-limit/webscr.cmd-login-submit"

我的要求是在上面的 url 中找到 '/' 的第 3 次出现的索引并做 substring 并且只取paypal-info.com 主机名Postgresql Query

任何关于实现这一点的想法将不胜感激。谢谢

Mic*_*ann 5

您尝试过 split_part 方法吗?

SELECT split_part('http://paypal-info.com/home.webapps.cgi-bin-limit/webscr.cmd-login-submit', '/', 3)
Run Code Online (Sandbox Code Playgroud)

结果:

split_part
paypal-info.com
Run Code Online (Sandbox Code Playgroud)

对于其他字符串函数,请尝试此文档: http ://www.postgresql.org/docs/9.1/static/functions-string.html

编辑:至于 indexOf 本身,我不知道任何内置的 postgres 解决方案。但是使用两个字符串函数你可以这样实现:

SELECT strpos('http://paypal-info.com/home.webapps.cgi-bin-limit/webscr.cmd-login-submit', split_part('http://paypal-info.com/home.webapps.cgi-bin-limit/webscr.cmd-login-submit', '/', 4)) - 1 as index_of;
Run Code Online (Sandbox Code Playgroud)