【问题标题】:Truncate letters from the end in postgres?在postgres中从末尾截断字母?
【发布时间】:2021-04-21 14:20:41
【问题描述】:

我在表中有 col A,我想截断 col A 值中最后一个数字之后的所有字母并创建 col B。

Col A Col B
A1234ABC A1234
B1234AB B1234

如何在 postgres 中将 col A 转换为 col B。(最后的字母长度不确定它可以是 3 还是 2。)

【问题讨论】:

  • 与您的问题无关,但是:Postgres 9.2 是no longer supported,您应该尽快计划升级。

标签: postgresql postgresql-9.2


【解决方案1】:

您可以将substring() 与正则表达式一起使用

select substring(col_a from '^.*[0-9]+')
from the_table

它从一开始就提取所有内容,直到至少一位或多位数字。对于没有数字的输入值,它将“失败”。例如'ABCD' 作为输入将产生一个空字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多