我试图检查从列中排除字符串模式的最佳和最佳方法,而不影响实际数据。
在Redshift DW中,我有一个表列公司,它的某些记录以不同的方式以INC结尾,因此希望排除INC的字符串模式,只捕获公司名称。请参阅下面的样本数据和预期输出。
WITH T AS (
select 'Cincin,Inc' id
union all
select 'Tinc, INc.' id
union all
select 'Cloud' id
union all
select 'Dinct Inc.' id
)
select id , regexp_replace(id,{exp}) from T
/**OutPut***/
Cincin
Tinc
Cloud
Dinct发布于 2019-07-01 23:45:53
Redshift不支持regex不区分大小写,但考虑到目标字符串很小,您可以使用[Ii][Nn][Cc]不太痛苦地绕过它。
regexp_replace(id, ',? *[Ii][Nn][Cc]\.?$', '')见现场演示。
测试:
WITH T AS (
select 'Cincin,Inc' id
union all
select 'Tinc, INc.' id
union all
select 'Cloud' id
union all
select 'Dinct Inc.' id
)
select id , regexp_replace(id, ',? *[Ii][Nn][Cc]\.?$', '') from T输出:
Cincin
Tinc
Cloud
Dinct发布于 2019-07-01 23:25:50
尝试替换模式,?\s*Inc\.?$
select id, regexp_replace(id, ',?\\s*[Ii][Nn][Cc]\\.?$', '') from T发布于 2019-07-01 23:44:49
如果你对这个案子没有分寸,你可以用它。
WITH T AS (
select 'Cincin,Inc' id
union all
select 'Tinc, INc.' id
union all
select 'Cloud' id
union all
select 'Dinct Inc.' id )
select id , regexp_replace(lower(iD),'[^a-z]+(inc)([^a-z])*','')
from T产出:
id regexp_replace
Cincin,Inc cincin
Tinc, INc. tinc
Cloud cloud
Dinct Inc. dincthttps://stackoverflow.com/questions/56843485
复制相似问题