在Oracle中提取单元格数据时出现查询错误

e4eetjau  于 2023-04-29  发布在  Oracle
关注(0)|答案(2)|浏览(131)

我想从一个列的字段中获取一个值,这个值有时会变化,目的是使数据均匀。我得到ORA-00911: invalid character。我不知道我错过了什么?
| K.C93FECHA_APL|K.93FECHA_APL|
| --------------|--------------|
| ARONDON|C26cupo:5000000|
| BCRUZ|C26cupo:20000000|
| BMARIN|C26cupo:150000000 C26edadretiroforzoso:70 C26limimite edad:68|
注意,最后一条记录不仅有字段C26cupo:,还有C26edadretiroforzoso:C26limimite edad:,并且在“:”之间有一个空格。
我应该可以得到这个:
| K.C93FECHA_APL|K.93FECHA_APL|
| --------------|--------------|
| ARONDON|C26cupo:5000000|
| BCRUZ|C26cupo:20000000|
| BMARIN|C26cupo:15000000|
我用了这个代码。

SELECT 
  K.C93USUARIO, 
  K.C93FECHA_APL, 
  CONCAT('C26cupo:', TO_NUMBER(SUBSTR(K.C93VALANTES, INSTR(K.C93VALANTES, 'C26cupo:') + LENGTH('C26cupo:')))) AS C93VALANTES
FROM t93_log K
WHERE K.C93USUARIO IN ('vkmateus918')
  AND K.C93FECHA_APL >= TO_DATE('2022/10/01', 'yyyy/mm/dd')
  AND K.C93FECHA_APL <= TO_DATE('2022/10/01', 'yyyy/mm/dd')
  AND K.C93IDPROCESO IN ('pagaduriabean') 
  AND K.C93TRANSACCION IN ('UPDATE')
  AND K.C93VALANTES NOT LIKE '%CONSOLIDADORA%'
  AND K.C93VALANTES NOT LIKE '%C26OPCIONCAMPO2 :%'
  AND K.C93CAMPO LIKE '%C26CUPO%'
  AND INSTR(K.C93VALANTES, 'C26cupo:') > 0

但我得到了上述错误

7fhtutme

7fhtutme1#

这个可以吗参见第7行。

SQL> with test (col1, col2) as
  2    (select 'ARONDON', 'C26cupo : 5000000' from dual union all
  3     select 'BCRUZ'  , 'C26cupo : 200000000' from dual union all
  4     select 'BMARIN' , 'C26cupo : 150000000 C26edadretiroforzoso : 70 C26limimite edad : 68' from dual
  5    )
  6  select col1,
  7    regexp_substr(col2, 'C26cupo : \d+') result
  8  from test;

COL1    RESULT
------- ------------------------------
ARONDON C26cupo : 5000000
BCRUZ   C26cupo : 200000000
BMARIN  C26cupo : 150000000

SQL>
6mzjoqzu

6mzjoqzu2#

如果你不知道/有/想要regexp(或者它太慢),你可以尝试使用using SubStr() and InStr()函数:

WITH        -- Sample data
    tbl (A_NAME, FLDS) as
        (   select 'ARONDON', 'C26cupo : 5000000' from dual union all
            select 'BCRUZ'  , 'C26cupo : 200000000' from dual union all
            select 'BMARIN' , 'C26cupo : 150000000 C26edadretiroforzoso : 70 C26limimite edad : 68' from dual
        )
--
--  Main SQL
Select  A_NAME,
  -- get substring starting from position 1 with length defined by position of the first blank ' ' after the position of ' : ' plus 3 characters
        SubStr( FLDS || ' ', 1, InStr(FLDS || ' ', ' ', InStr(FLDS || ' ', ' : ', 1, 1) + 3) - 1 ) "FLD"  
  -- the -1 at the end excludes mentioned blank space from the result (You can use RTRIM() instead)
From    tbl
--
--  R e s u l t :
A_NAME  FLD                   
------- ----------------------
ARONDON C26cupo : 5000000     
BCRUZ   C26cupo : 200000000   
BMARIN  C26cupo : 150000000

注意:在代码中有一个空格字符添加到FLDS列,以获取在':'字符串的一部分。..

相关问题