【问题标题】:SQL - get specific text from stringSQL - 从字符串中获取特定文本
【发布时间】:2019-12-14 20:54:54
【问题描述】:

我有这个字符串的列:

['autre__renvoi_vers_un_mail', 'contact_type__email']
['contact_type__email', 'internal__shop', 'uk']

我只需要从字符串中获取contact_type__* 部分(每行仅出现一次)

contact_type__email
contact_type__email

有什么建议吗?

【问题讨论】:

    标签: sql regex google-bigquery


    【解决方案1】:

    你可以使用regexp function regexp_extract(),比如:

    regexp_extract(mycol, 'contact_type__[a-zA-Z0-9_]*')
    

    这将匹配字符串'contact_type__,后跟一系列字母、数字或下划线(通常定义为单词字符)。

    【讨论】:

    • @OksanaOk:欢迎!只是检查一下,如果您使用\\w 而不是\w 怎么办?
    【解决方案2】:

    举个例子:

    create table t (col varchar(100));
    insert into t values
    ('[''autre__renvoi_vers_un_mail'', ''contact_type__email'']'),
    ('[''contact_type__email'', ''internal__shop'', ''uk'']');
    

    你可以使用:

    select
        substring 
        (
         col,
         charindex('contact_type', col) + 14,
         charindex('''', col, charindex('contact_type', col)) - charindex('contact_type', col) - 14
        )
    from
        t;
    

    db小提琴here

    【讨论】:

      猜你喜欢
      • 2015-01-02
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多