postgresqlsnowflake-cloud-data-platformcharacter-trimmingpostgresql-13

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:

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

See:

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

However, I need to rtrim to only remove if the trimming text is an exact match. How do I do that?


Solution

  • The order of the characters doesn't matter for rtrim().
    rtrim('rtrimtest', 'best') is the same as rtrim('rtrimtest', 'stbe')

    On Postgres you can use regex_replace() for what you want to do:

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

    Online example