如何在PostgreSQL中第4次出现字符之前切断字符串? [英] How to cut off strings before 4th occurrence of a character in postgresql?

查看:91
本文介绍了如何在PostgreSQL中第4次出现字符之前切断字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在许多数据库中,charindex函数可以接受在第三次出现时获取字符,但是postgresql中的strpos不接受它。

I know in a lot of databases the charindex function can accept getting the character on the third occurrence but the strpos in postgresql doesn't accept it.

基本上我'd需要剪切掉第四个空格(包括最后一个空格)之后的所有内容

Basically I'd need to cut everything after the 4th space (including the last space)

如果我有这样的字符串:

If I have a string like:

FLAT 11, ELMER HOUSE 33-35 

如何在 HOUSE之后将其切成公正的样子:

How to cut it after the 'HOUSE' to turn it into just:

FLAT 11, ELMER HOUSE

不,使用left无效,因为这些字符串非常可变。

And no, using left won't work because these strings are very variable.

推荐答案

这里是一种方法:

select substring(str || '    ' from '^[^ ]* [^ ]* [^ ]*')

这会查找组字符(可能为空)之间用空格隔开。

This looks for groups of characters (possibly empty) separated by a space. It goes for everything up to the fourth space in str.

||中的所有内容都可以用到 str 中的第四位。 ’只是为了确保字符串上有四个空格。否则,模式将返回 NULL

这篇关于如何在PostgreSQL中第4次出现字符之前切断字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆