甲骨文的常规exp

时间:2020-03-25 15:48:52

标签: regex oracle

我有一个表,该表具有这样的列值。

ERROR - abc failed to load service request on {Date/Time} .The CIRCUIT is missing.    
ERROR - abc failed to load service request on {Date/Time}. The JOB_STREET_NAME is missing.    
ERROR - abc failed to load service request on {Date/Time}. The JOB_TOWN is missing.

我想在我的Oracle软件包的特定列中找到{日期/时间}部分,并将其替换为CURRENT_TIMESTAMP。

找不到对我有用的任何东西。我是新来的。寻找建议。

2 个答案:

答案 0 :(得分:0)

我认为您可以使用REGEXP_SUBSTR来实现它,如下所示:

SELECT 
    REGEXP_SUBSTR(YOUR_COL,'failed to load service request on(.+) \.',1,1,NULL,1) 
FROM YOUR_TABLE

答案 1 :(得分:0)

一个简单的REPLACE函数似乎可以正常工作。

SQL> -- create an populate table
SQL> create table errmsg (msg_id number,
  2                       msg_txt varchar2(100)
  3                      )
  4  ;

Table created.

SQL> insert into errmsg values (1,'ERROR - abc failed to load service request on {Date/Time} .The CIRCUIT is missing.');

1 row created.

SQL> insert into errmsg values (2,'ERROR - abc failed to load service request on {Date/Time}. The JOB_STREET_NAME is missing.');

1 row created.

SQL> insert into errmsg values (3,'ERROR - abc failed to load service request on {Date/Time}. The JOB_TOWN is missing.');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from errmsg;

    MSG_ID
----------
MSG_TXT
--------------------------------------------------------------------------------
         1
ERROR - abc failed to load service request on {Date/Time} .The CIRCUIT is missin
g.


ERROR - abc failed to load service request on {Date/Time} .The CIRCUIT is missin
g.

         2
ERROR - abc failed to load service request on {Date/Time}. The JOB_STREET_NAME i
s missing.

         3
ERROR - abc failed to load service request on {Date/Time}. The JOB_TOWN is missi
ng.


3 rows selected.

SQL> -- test the method
SQL> select replace(msg_txt,'{Date/Time}',to_char(sysdate,'dd-Mon-yyyy hh24:mi:ss')) logged_message
  2  from errmsg
  3  where msg_id=1;

LOGGED_MESSAGE
--------------------------------------------------------------------------------
ERROR - abc failed to load service request on 25-Mar-2020 16:16:39 .The CIRCUIT
is missing.


1 row selected.

SQL> --
SQL> select replace(msg_txt,'{Date/Time}',to_char(sysdate,'dd-Mon-yyyy hh24:mi:ss')) logged_message
  2  from errmsg
  3  where msg_id=2;

LOGGED_MESSAGE
--------------------------------------------------------------------------------
ERROR - abc failed to load service request on 25-Mar-2020 16:16:39. The JOB_STRE
ET_NAME is missing.


1 row selected.

SQL> --
SQL> select replace(msg_txt,'{Date/Time}',to_char(sysdate,'dd-Mon-yyyy hh24:mi:ss')) logged_message
  2  from errmsg
  3  where msg_id=3;

LOGGED_MESSAGE
--------------------------------------------------------------------------------
ERROR - abc failed to load service request on 25-Mar-2020 16:16:39. The JOB_TOWN
 is missing.


1 row selected.

SQL> --
SQL> -- drop the table
SQL> drop table errmsg purge;

Table dropped.

SQL> spool off