【问题标题】:REGEXP_SUBSTR converted output is not casting as integerREGEXP_SUBSTR 转换后的输出未转换为整数
【发布时间】:2014-05-06 17:37:36
【问题描述】:

我尝试使用如下所示的 REGEXP_SUBSTR Sql 函数从 20 个字符的字符串中提取所有数字。

select
REGEXP_SUBSTR(substring(mycolumn,1,20), '^[0-9]', 1)
|| REGEXP_SUBSTR(substring(mycolumn,1,20), '^[0-9]', 2)
|| REGEXP_SUBSTR(substring(mycolumn,1,20), '^[0-9]', 3)
...
...
|| REGEXP_SUBSTR(substring(mycolumn,1,20), '^[0-9]', 20)
from tbl;

但是当尝试将其转换为 bigint / decimal 或任何数字数据类型时,它会以 Invalid input syntax for type numericInvalid digit, Value '2', Pos 0, Type: Long 失败 等等。

我错过了什么吗? REGEXP_SUBSTR 连接的典型输出为 105622,0044,022 等。查询在 Redshift 数据仓库上运行,但 REGEXP_REPLACE/TRANSLATE 尚不存在。

【问题讨论】:

    标签: sql regex casting integer amazon-redshift


    【解决方案1】:

    看起来只要字符串中没有数字,那么上述 regexp_substr 的串联将返回不支持转换为整数的空字符串 ('')。所以我用了NULLIF (regex expression, '')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 2017-06-20
      • 2016-03-06
      相关资源
      最近更新 更多