如何从SnowFlake中的字符串中提取数字

huangapple go评论56阅读模式
英文:

How to extract number from a string in SnowFlake

问题

我有一个字符串需要提取卡号,

用户通过门进入,他的卡号为(123456)其他文本

由于只有一个括号,我能够提取卡号。我使用的解决方案是

> 选择"用户通过门进入,他的卡号为(123456)。"
> 作为card_number_text,substring(card_number_text,POSITION('(',
> card_number_text)+1,POSITION(')',card_number_text) - POSITION('(',
> card_number_text) - 1)

这样可以正确提取卡号。但我面临的问题是当文本中有两个括号时,

例如,这样的文本不会提供正确的卡号

用户通过门进入(使用反通行)并且他的卡号为(123456)其他文本
英文:

I have a string like this to extract the card number,

A user entered through a door and his Card number (123456) other text

Since there is only one parenthesis,I am able to extract the card number. The solution for this I am using is

> Select "An user entered through a door and his Card number (123456)."
> as card_number_text, substring(card_number_text, POSITION('(',
> card_number_text)+1, POSITION(')', card_number_text) - POSITION('(',
> card_number_text) - 1)

This gives the card number properly. But the problem I facing is when there are two parentheses in the text,

For example, a text like this won't give a proper card number

A user entered through a door (using an anti-pass) and his Card number (123456) other text

答案1

得分: 1

为什么不根据字符串“Card number(”拆分数据,然后在进行一些清理后选择第二部分作为卡号。

select 
replace(                               -- 这会去除额外字符并返回123456
split_part(data_col,'Card number (',2) -- 这会得到123456)。
,').') as card_number 

无论卡号是数字还是字母数字都无关紧要。

英文:

Why not splitting the data based on string Card number (. And then pick up second part as card number after little cleanup.

select 
replace(                               -- this removes extra character and returns 123456
split_part(data_col,'Card number (',2) -- this results 123456).
,').') as card_number 

It doesnt matter if card number is a numeric or alphanumeric.

huangapple
  • 本文由 发表于 2023年5月30日 09:20:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定