首页 > 解决方案 > POSTGRESQL 位置子串

问题描述

我有一个简单的问题(我认为)我无法解决:我有很多这样的标签: 'FOOT05_00120_RIG_1_GOAL_K' 但是所有记录的长度都不一样,我需要一个查询来获取最后一个之前的所有字符_,有人吗我能怎么做?

(它可能在这样的查询中:

SELECT tag FROM Football

)

谢谢大家

标签: sqlpostgresqlpositionsubstring

解决方案


这个问题似乎几乎是为regexp_replace()

select regexp_replace(tag, '(.*)_[^_]*$', '\1')
from football;

正则表达式被解释为:

'(.*)_[^_]*$'
-^ remember this expression for a reference
--^ any character
---^ any number of times
----^ followed by _
-----^ followed by any characters that are not _
----------^ any number of times
-----------^ at the end of the string

推荐阅读