【问题标题】:Replace spaces and hyphens with null and check the length in Oracle [closed]用 null 替换空格和连字符并检查 Oracle 中的长度 [关闭]
【发布时间】:2022-06-19 16:40:24
【问题描述】:

我想在删除空格和连字符后检查值的长度,并在数据库中保存不带空格但带有连字符的值。

我的逻辑如下:

IF(LENGTH(REPLACE(REGEXP_REPLACE(myValue,'[[:space:]]*',''),'-','')=13)
THEN
 --myValue := REGEXP_REPLACE(myValue,'[[:space:]]*',''); 
 --Insert...myValue
END IF;

但是 if 条件失败并且数据没有被保存。我错过了什么?

【问题讨论】:

  • 与java无关
  • 看起来像delphi/pascal
  • 请提供未通过此检查的示例数据。我们应该怎么知道?例如,除空格和连字符外的任何符号重复 20 次显然会失败此检查

标签: regex oracle


【解决方案1】:

我认为您不需要正则表达式。

对于这样的样本数据,查询返回如下结果:

SQL> with sample_data (id, col) as
  2    (select 1, 'abc def-ghi123 4' from dual union all
  3     select 2, '12  -xyz--238-62' from dual)
  4  select id,
  5         col,
  6         replace(replace(col, ' '), '-') val,
  7         --
  8         case when length(replace(replace(col, ' '), '-')) = 13 then 'Y' else 'N' end cb_insert,
  9         replace(col, ' ') val_to_insert
 10  from sample_data;

        ID COL              VAL              C VAL_TO_INSERT
---------- ---------------- ---------------- - ----------------
         1 abc def-ghi123 4 abcdefghi1234    Y abcdef-ghi1234   --> insert
         2 12  -xyz--238-62 12xyz23862       N 12-xyz--238-62   --> don't insert

SQL>
  • val 是去掉空格和连字符的字符串
  • cb_insert:如果是Y,则该值应作为删除空格和连字符的字符串长度插入,等于13
  • val_to_insert: 删除了空格的源字符串

所以,对于目标表:

SQL> create table test (id number, value varchar2(20));

Table created.

insert 查询看起来像这样(重复使用上面的查询):

SQL> insert into test (id, value)
  2    with
  3    sample_data (id, col) as
  4      (select 1, 'abc def-ghi123 4' from dual union all
  5       select 2, '12  -xyz--238-62' from dual
  6      ),
  7    temp as
  8      (select id,
  9              col,
 10              replace(replace(col, ' '), '-') val,
 11              --
 12              case when length(replace(replace(col, ' '), '-')) = 13 then 'Y' else 'N' end cb_insert,
 13              replace(col, ' ') val_to_insert
 14       from sample_data
 15      )
 16    select id, val_to_insert
 17    from temp
 18    where cb_insert = 'Y';

1 row created.

结果:

SQL> select * From test;

        ID VALUE
---------- --------------------
         1 abcdef-ghi1234

SQL>

【讨论】:

    【解决方案2】:

    但是 if 条件失败并且数据没有被保存。

    你有两个潜在的问题:

    1. 如果您的字符串完全是空白字符或连字符,那么它们都将被替换,您将得到一个空字符串。在 Oracle 中,空字符串与 NULLLENGTH(NULL) 输出相同,NULLNULL=<anything> 始终为 false。这可能不是问题,但可能会导致意外结果。
    2. 检查不带空格或连字符的长度是否为 13,然后插入不带空格但带连字符的数据。如果您有一列可以存储 13 个(甚至 16 个)字符,并且您检查输入是否适合没有连字符但随后您允许未知数量的连字符,那么您的 INSERT 将失败,因为该值对于列。
    DECLARE
      myValue VARCHAR2(50);
      mv_no_spaces VARCHAR2(50);
    BEGIN
      myValue := '   abcde-fgh-ijk----lm  ' || CHR(9) || CHR(10) || CHR(13) || '   ';
      mv_no_spaces := REGEXP_REPLACE(myValue,'[[:space:]]*');
    
      IF  LENGTH(REPLACE(mv_no_spaces,'-','')) = 13
      AND LENGTH(mv_no_spaces) <= 20 -- or whatever your column size is.
      THEN
        INSERT INTO your_table (your_column) VALUES (mv_no_spaces);
      END IF;
    END;
    /
    

    db小提琴here

    【讨论】:

      猜你喜欢
      • 2015-07-10
      • 2015-10-22
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      相关资源
      最近更新 更多