【问题标题】:How to get substring based on a character and starting to read the string from the right如何根据字符获取子字符串并从右侧开始读取字符串
【发布时间】:2019-06-17 11:05:14
【问题描述】:

我在一列上有以下值:

DB3-0800-VRET,
DB3-0800-IC,
IB-TZ-850-IB,
O11FS-OB ...

我想从每个值中删除破折号后的最后一部分。 我需要得到以下结果:

DB3-0800-VRET -> DB3-0800,
DB3-0800-IC   -> DB3-0800,
O11FS-OB      -> O11FS

我尝试使用 RedShift 的 SPLIT_PART 函数,但没有运气。 如果有人知道正则表达式来选择我需要的部分,我将不胜感激。

【问题讨论】:

  • Redshift 还是 Postgres?这是两个非常不同的 DBMS(尽管 Redshift 基于 Postgres)
  • @a_horse_with_no_name 它是 Redshift,但因为我知道 Redshift 基于 Postgres,所以我也将它包含在标签中。

标签: sql regex amazon-redshift


【解决方案1】:

在 Postgres 和 Redshift 中,您应该都可以使用 regexp_replace():

select regexp_replace(str, '-[^-]+$', '')

【讨论】:

  • 感谢您的帮助。在 redshift 中,此查询无效。当我删除最后一个引号时,我只得到我想要删除的部分。你能告诉我谁可以选择子字符串而不包括我现在得到的部分。
  • @PlarentHaxhidauti 。 . .它应该可以工作:docs.aws.amazon.com/redshift/latest/dg/REGEXP_REPLACE.html.
  • 谢谢 我在查询中犯了拼写错误。它正在工作:))
猜你喜欢
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
  • 2012-12-28
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多