【问题标题】:How can I count the number of instances of this character at the end of a string in SQL or PL/SQL?如何计算 SQL 或 PL/SQL 中字符串末尾的此字符的实例数?
【发布时间】:2013-08-04 10:15:54
【问题描述】:

我有一个以一定数量的“=”字符结尾的字符串。它基本上是一个 base 64 字符串。

我如何才能得到最后的“=”字符数?最好使用内置的 SQL 函数或正则表达式。

我知道instr 函数,但它似乎不能在这里应用。我不确定正则表达式是否也适用于此。

【问题讨论】:

    标签: sql regex oracle plsql


    【解决方案1】:

    使用lengthreplace

    select length(some_column) - length(replace(some_column, '=', ''))
    from your_table
    

    【讨论】:

    • 谢谢!最终使用 select nvl(length(replace('hello world==', rtrim('hello world==', '='), '')), 0) padding from dual
    【解决方案2】:
    SELECT REGEXP_COUNT('hello world==', '=') cnt FROM dual
    /
    
    
    SELECT  count(distinct(Instr('hello world==','=', LEVEL))) cnt
      FROM dual
    CONNECT BY LEVEL <= Length('hello world==') 
    ORDER BY 1
    /
    

    【讨论】:

    • 第一行计算所有的 '=' 而不仅仅是最后的那些,例如regexp_count('==Hello world ==','=') 返回 4。OP 想要最后的字符数。
    猜你喜欢
    • 2011-10-23
    • 2010-12-24
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    相关资源
    最近更新 更多