在 SQL (Snowflake) 中使用正则表达式从字符串中提取
Extract from string using regex in SQL (Snowflake)
我有一个包含一长串文本的数据库字段:
Date: 02/03/22 Customer Price: 500 Total Price: 701.56 Paid to Date: 304.10 Confirmed: TRUE
从该字符串中提取特定数字的最佳方法是什么?例如,如何提取 'Total Price' (701.56) 和 'Paid to Date' (304.10)?我想我需要使用 REGEXP_SUBSTR
但我不是 100% 确定。例如:
select REGEXP_SUBSTR(my_field_name, 'Total Price: (SOME-REGEX)') as total_price,
select REGEXP_SUBSTR(my_field_name, 'Paid To Date: (SOME-REGEX)') as paid_to_date,
from my_table
我只需要return值,不需要前面的文字。
SELECT 'Date: 02/03/22 Customer Price: 500 Total Price: 701.56 Paid to Date: 304.10 Confirmed: TRUE' as a ,
REGEXP_SUBSTR(a,'Total Price: [0-9]*.[0-9]*') as total_price,
REGEXP_SUBSTR(a,'Paid to Date: [0-9]*.[0-9]*') as paid_to_date;
这是一种方法:
set my_string='Date: 02/03/22 Customer Price: 500 Total Price: 701.56 Paid to Date: 304.10 Confirmed: TRUE';
select REGEXP_SUBSTR($my_string, 'Total Price: (\d*.\d*)',1,1,'e') as total_price;
select REGEXP_SUBSTR($my_string, 'Paid to Date: (\d*.\d*)',1,1,'e') as paid_to_date;
结果:
701.56
304.10
我有一个包含一长串文本的数据库字段:
Date: 02/03/22 Customer Price: 500 Total Price: 701.56 Paid to Date: 304.10 Confirmed: TRUE
从该字符串中提取特定数字的最佳方法是什么?例如,如何提取 'Total Price' (701.56) 和 'Paid to Date' (304.10)?我想我需要使用 REGEXP_SUBSTR
但我不是 100% 确定。例如:
select REGEXP_SUBSTR(my_field_name, 'Total Price: (SOME-REGEX)') as total_price,
select REGEXP_SUBSTR(my_field_name, 'Paid To Date: (SOME-REGEX)') as paid_to_date,
from my_table
我只需要return值,不需要前面的文字。
SELECT 'Date: 02/03/22 Customer Price: 500 Total Price: 701.56 Paid to Date: 304.10 Confirmed: TRUE' as a ,
REGEXP_SUBSTR(a,'Total Price: [0-9]*.[0-9]*') as total_price,
REGEXP_SUBSTR(a,'Paid to Date: [0-9]*.[0-9]*') as paid_to_date;
这是一种方法:
set my_string='Date: 02/03/22 Customer Price: 500 Total Price: 701.56 Paid to Date: 304.10 Confirmed: TRUE';
select REGEXP_SUBSTR($my_string, 'Total Price: (\d*.\d*)',1,1,'e') as total_price;
select REGEXP_SUBSTR($my_string, 'Paid to Date: (\d*.\d*)',1,1,'e') as paid_to_date;
结果:
701.56
304.10