简体   繁体   English

精确匹配 rtrim function 中的 trimming_text

[英]Extact match the trimming_text in the rtrim function

If I run the following query in Postgres or Snowflake, it will remove test from the end of the input string, even though the trimming text is best :如果我在 Postgres 或 Snowflake 中运行以下查询,它将从输入字符串的末尾删除test ,即使修剪文本是best的:

SELECT rtrim('rtrimtest', 'best');

See:看:

https://www.db-fiddle.com/f/kKYwe5tNLpVoacM2q1nJY7/0 https://www.db-fiddle.com/f/kKYwe5tNLpVoacM2q1nJY7/0

However, I need to rtrim to only remove if the trimming text is an exact match .但是,我需要 rtrim 以仅在修剪文本完全匹配时删除。 How do I do that?我怎么做?

The order of the characters doesn't matter for rtrim(). rtrim() 的字符顺序无关紧要。
rtrim('rtrimtest', 'best') is the same as rtrim('rtrimtest', 'stbe') rtrim('rtrimtest', 'best')rtrim('rtrimtest', 'stbe')相同

On Postgres you can use regex_replace() for what you want to do:在 Postgres 上,你可以使用 regex_replace() 来做你想做的事情:

regexp_replace('rtrimtest', 'best$', '');

Online example 在线示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM