如何在PostgreSQL中第4次出现字符之前切断字符串?

编程入门 行业动态 更新时间:2024-10-28 08:17:00
本文介绍了如何在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 中的第四位。 ’

更多推荐

如何在PostgreSQL中第4次出现字符之前切断字符串?

本文发布于:2023-10-16 17:08:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   如何在   PostgreSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!