从雪花中的字符串中提取特定文本

时间:2020-12-28 15:52:46

标签: snowflake-cloud-data-platform snowflake-schema

我是雪花的新手。

输入字符串:["http://info.wealthenhancement.com/ppc-rt-retirement-planning"]

输出字符串:info.wealthenhancement.com/ppc-rt-retirement-planning

请帮忙获取输出字符串。

谢谢

2 个答案:

答案 0 :(得分:2)

通过使用 // 作为分隔符进行拆分,这将适用于 http 和 https URL。只需要最后一条语句。另外两个展示了它是如何在步骤中完成的:

-- Set a session variable to the string
set INPUT_STRING = '["http://info.wealthenhancement.com/ppc-rt-retirement-planning"]';

-- Trim leading and trailing square brackets and double quotes
select (trim($INPUT_STRING, '"[]'));

-- Split using // as a delimiter and keep only the right part and cast as string
select split((trim($INPUT_STRING, '"[]')), '//')[1]::string as URL

答案 1 :(得分:1)

使用 substr 函数只取第 8 个字符到结尾的字符:

select 
    'http://info.wealthenhancement.com/ppc-rt-retirement-planning' as orig_value,
    substr(orig_value, 8) as new_value

输出为:

+-------------------------------------------------------------+-------------------------------------------------------+
|ORIG_VALUE                                                   | NEW_VALUE                                             |
+-------------------------------------------------------------+-------------------------------------------------------+
|http://info.wealthenhancement.com/ppc-rt-retirement-planning | info.wealthenhancement.com/ppc-rt-retirement-planning |
+-------------------------------------------------------------+-------------------------------------------------------+
相关问题