【问题标题】:Extract only the first set of digits from Redshift/PostgreSQL仅从 Redshift/PostgreSQL 中提取第一组数字
【发布时间】:2017-02-13 03:56:38
【问题描述】:

我的 RedShift 列中有以下字符串 = col_name:

Lorem ipsum dolor sit amet-80 consectetuer adipiscing elit。 Aenean commodo ligula eget dolor,, 80 Cras dapibus。 Vivamus elementum semper nisi。

我只需要提取的第一个数字作为新列。我试过了

regexp_replace(item_name,'[^[:digit:]]') as new_col

但我得到8080

我怎样才能得到80

【问题讨论】:

    标签: postgresql amazon-redshift


    【解决方案1】:

    检查是否支持regexp_substr

    select regexp_substr(col,'\\d+') from t;
    

    如果没有,试试这个:

    select regexp_replace(col,'^\\D*(\\d+).*$', '\1') from t
    

    【讨论】:

    • regexp_substr 受支持,但均无效。对于 regexp_substr(col,'\d+') 我得到“d”作为结果。对于 regexp_replace(col,'^\D*(\d+).*$', '\1') 我得到了整个字符串。谢谢!
    • @Hema 你现在可以试试吗?我认为Redshift requires two slashes
    • 这有效 regexp_substr(item_name,'\\d+')。谢谢楼主!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    相关资源
    最近更新 更多