我想删除以减号结尾的列中的最后一个字符。我怎样才能在postgresql中做到这一点?
例如:
sdfs-dfg4t-etze45z5z- => sdfs-dfg4t-etze45z5z gsdhfhsfh-rgertggh => stay untouched
我可以使用一种简单的语法吗?
如果可以删除所有结尾的破折号,请使用修剪功能;如果仅需要删除最后的破折号,请使用regexp_replace。修剪可能比regexp_replace更好。
with strings as ( select 'sdfs-dfg4t-etze45z5z-' as string union all select 'sdfs-dfg4t-etze45z5z--' as string union all select 'gsdhfhsfh-rgertggh' ) select string, trim(trailing '-' from string) as all_trimmed, regexp_replace(string, '-$', '') as one_trimmed from strings
结果:
string all_trimmed one_trimmed sdfs-dfg4t-etze45z5z- sdfs-dfg4t-etze45z5z sdfs-dfg4t-etze45z5z sdfs-dfg4t-etze45z5z-- sdfs-dfg4t-etze45z5z sdfs-dfg4t-etze45z5z- gsdhfhsfh-rgertggh gsdhfhsfh-rgertggh gsdhfhsfh-rgertggh