Postgresql:如果列以减号结尾,则删除文本字段中的最后一个字符 [英] Postgresql: Remove last char in text-field if the column ends with minus sign
本文介绍了Postgresql:如果列以减号结尾,则删除文本字段中的最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果以减号结尾,我想删除列中的最后一个字符.我怎么能在 postgresql 中做到这一点?
I want to remove the last char in a column if it ends with the minus sign. How could I do this in postgresql?
例如:
sdfs-dfg4t-etze45z5z- => sdfs-dfg4t-etze45z5z
gsdhfhsfh-rgertggh => stay untouched
有我可以使用的简单语法吗?
Is there an easy syntax I can use?
推荐答案
如果可以删除所有尾随的破折号,请使用 trim 函数,或者如果您只需要删除最后一个破折号,请使用 regexp_replace.Trim 的性能可能比 regexp_replace 好.
Use the trim function if all trailing dashes can be removed, or use regexp_replace if you need only the last dash removed. Trim probably performs better than 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
这篇关于Postgresql:如果列以减号结尾,则删除文本字段中的最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文