Cut string after first occurrence of a character

ε祈祈猫儿з 提交于 2019-12-04 05:02:55
Erwin Brandstetter

Use split_part():

SELECT split_part('first:last', ':', 1) AS first_part

Returns the whole string if the delimiter is not there. And it's simple to get the 2nd or 3rd part etc.

Substantially faster than functions using regular expression matching. And since we have a fixed delimiter we don't need the magic of regular expressions.

Related:

regexp_replace() may be overload for what you need, but it also gives the additional benefit of regex. For instance, if strings use multiple delimiters.

Example use:

select regexp_replace( 'first:last', E':.*', '');

SQL Select to pick everything after the last occurrence of a character

select right('first:last', charindex(':', reverse('first:last')) - 1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!