我想删除列中的最后一个字符(如果它以减号结尾)。
我怎样才能在 postgresql 中做到这一点?
例如:
sdfs-dfg4t-etze45z5z- => sdfs-dfg4t-etze45z5z
gsdhfhsfh-rgertggh => stay untouched
我可以使用简单的语法吗?
如果可以删除所有尾随破折号,请使用修剪函数,或者如果您只需要删除最后一个破折号,请使用 regexp_replace。 Trim 的性能可能比 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)