REGEXP_SUBSTR converted output is not casting as integer

拜拜、爱过 提交于 2020-01-04 15:52:20

问题


I tried extracting all the digits out of a 20 character string by using REGEXP_SUBSTR Sql function like below.

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;

But when trying to cast it as bigint / decimal or any numeric data type it is failing with Invalid input syntax for type numeric or Invalid digit, Value '2', Pos 0, Type: Long and so on.

Am I missing some thing? Typical output of the REGEXP_SUBSTR concatenation are 105622,0044,022 etc. The query ran on Redshift datawarehouse and REGEXP_REPLACE/TRANSLATE is not yet present there.


回答1:


Looks like whenever there are no digits in the string, then the above concatenation of regexp_substr s is returning empty string ('') which is not supported for casting into integer. So I used a NULLIF (regex expression, '')



来源:https://stackoverflow.com/questions/22692833/regexp-substr-converted-output-is-not-casting-as-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!