【问题标题】:How to check a specific spot in a string in Oracle如何在 Oracle 中检查字符串中的特定位置
【发布时间】:2016-10-23 06:38:36
【问题描述】:

如何检查字符串中的第九个字符是字母还是数字。

例如,在“25006070TR”中,我想返回 Yes 或类似它是一个字母的东西。另一方面,在“250060708R”中,我想要一个“否”或其他数字。

【问题讨论】:

    标签: mysql oracle oracle11g


    【解决方案1】:

    您可以使用 case 语句以及 substr 和 regexp_like 的组合,例如:

    with sample_data as (select '25006070TR' str from dual union all
                         select '250060708R' str from dual union all
                         select '25006070#R' str from dual union all
                         select '25006070 R' str from dual union all
                         select '12345' str from dual)
    select str,
           case when regexp_like(substr(str, 9, 1), '\d') then 'Digit'
                when regexp_like(substr(str, 9, 1), '[[:alpha:]]') then 'Letter'
                when substr(str, 9, 1) is null then 'Empty'
                else 'Special character'
           end ninth_char_type
    from   sample_data;
    
    
    STR        NINTH_CHAR_TYPE  
    ---------- -----------------
    25006070TR Letter           
    250060708R Digit            
    25006070#R Special character
    25006070 R Special character
    12345      Empty            
    

    显然,如果您直接在 PL/SQL 中执行此操作,您可以直接将 case 语句嵌入到 PL/SQL 中;您不需要切换到 SQL。

    如果要从表中获取字符串,则应将 case 表达式保留在 SQL 语句中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-12
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 2013-12-08
      相关资源
      最近更新 更多