表中的CustomerCode排序是错乱的,我们要处理这批数据,
1.写SQL语句取CustomerCode列中后五位的数字结构的最大值2.生成新的编码方式插入到NewCode,编码方式要求采用C00001的形式,
其中C是固定的,后面五位按照CustomerCode列后五位的升序,从1开始

解决方案 »

  1.   

    substr函数
      

  2.   

    查询可以用这个语句:
    SELECT id, customercode, to_number(regexp_substr(customercode, '(\d)+$', 1, 1)),
           'C' ||
            to_char(row_number() over(ORDER BY to_number(regexp_substr(customercode, '(\d)+$', 1, 1))),
                    'fm09999') newcode
      FROM crm_customerinfo;
      

  3.   

    更新Newcode字段可以用这个语句:
    UPDATE crm_customerinfo a
       SET a.newcode =
            (SELECT newcode
               FROM (SELECT id, customercode, to_number(regexp_substr(customercode, '(\d)+$', 1, 1)),
                             'C' ||
                              to_char(row_number()
                                      over(ORDER BY to_number(regexp_substr(customercode, '(\d)+$', 1, 1))),
                                      'fm09999') newcode
                        FROM crm_customerinfo) b
              WHERE b.id = a.id);