从 Snowflake 中的列修剪值
Trimming value from a column in Snowflake
我有一个名为 File
的列,其值为 'Mens_Purchaser_Segment_Report'
和 'Loyalist_Audience_Segment_Report'
。我想捕获词段之前的所有内容。
我使用了查询:
select
TRIM(file,regexp_substr(file, '_Segment_Report.*')) as new_col
Output:
Mens_Purch
Loyalist_Audi
如何捕获 Segment 之前的所有内容?
已尝试以下但结果相同-->
TRIM(file,regexp_substr(file, 'S.*'))
TRIM(file,regexp_substr(file, '_S.*'))
尝试
使用regexp_replace
select regexp_replace(fld1, 'Segment', '') from (
select 'Mens_Purchaser_Segment_Report and Loyalist_Audience_Segment_Report' fld1 from dual );
您没有指定结尾文本是否始终为 _Segment_Report
,您要求的是 _Segment
之前的任何文本。取决于可以使用的各种解决方案,请参见下文。
create or replace table foo(s string) as select * from values
('Mens_Purchaser_Segment_Report'),
('Loyalist_Audience_Segment_Report');
-- If you know the suffix you want to remove is always exactly '_Segment_Report'
select s, replace(s, '_Segment_Report', '') from foo;
-- If you know the suffix you want to remove starts with '_Segment' but can have something after
-- - approach 1, where we replace the _Segment and anything after it with nothing
select s, regexp_replace(s, '_Segment.*', '') from foo;
-- - approach 2, where we extract things before _Segment
-- Note: it will behave differently if there are many instances of '_Segment'
select s, regexp_substr(s, '(.*)_Segment.*', 1, 1, 'e') from foo;
我有一个名为 File
的列,其值为 'Mens_Purchaser_Segment_Report'
和 'Loyalist_Audience_Segment_Report'
。我想捕获词段之前的所有内容。
我使用了查询:
select
TRIM(file,regexp_substr(file, '_Segment_Report.*')) as new_col
Output: |
---|
Mens_Purch |
Loyalist_Audi |
如何捕获 Segment 之前的所有内容?
已尝试以下但结果相同-->
TRIM(file,regexp_substr(file, 'S.*'))
TRIM(file,regexp_substr(file, '_S.*'))
尝试
使用regexp_replace
select regexp_replace(fld1, 'Segment', '') from (
select 'Mens_Purchaser_Segment_Report and Loyalist_Audience_Segment_Report' fld1 from dual );
您没有指定结尾文本是否始终为 _Segment_Report
,您要求的是 _Segment
之前的任何文本。取决于可以使用的各种解决方案,请参见下文。
create or replace table foo(s string) as select * from values
('Mens_Purchaser_Segment_Report'),
('Loyalist_Audience_Segment_Report');
-- If you know the suffix you want to remove is always exactly '_Segment_Report'
select s, replace(s, '_Segment_Report', '') from foo;
-- If you know the suffix you want to remove starts with '_Segment' but can have something after
-- - approach 1, where we replace the _Segment and anything after it with nothing
select s, regexp_replace(s, '_Segment.*', '') from foo;
-- - approach 2, where we extract things before _Segment
-- Note: it will behave differently if there are many instances of '_Segment'
select s, regexp_substr(s, '(.*)_Segment.*', 1, 1, 'e') from foo;