ORACLE feed regexp匹配功能

问题描述 投票:0回答:2

你如何将函数应用于正则表达式匹配?

我的目标是:将文本字段中的所有unis时间戳转换为人类可读的日期时间

Unixtimestamp可以通过提取

regexp (\d{10}' || chr(4) ||')

所以现在我有

SELECT 
   HISTODETAILS,
   regexp_replace(HISTODETAILS,'(\d{10}' || chr(4) ||')','\1')  HISTODETAILS2
FROM <table>
WHERE .....

我想将一个函数应用于\1

SELECT 
   HISTODETAILS,
   regexp_replace(HISTODETAILS,'(\d{10}' || chr(4) ||')',myfunction('\1'))  HISTODETAILS2
FROM <table>
WHERE .....

首先,你认为这可以做到吗?应该是这样的......怎么样? :)

TIA

编辑:我应该提到可能会出现多次时间戳,即

1398929938
this and that , bla bla bla

1400587008
more bla bla bla

1400587395
once more bla bla bla

最后的结果应该是

2014-05-01 09:38:58
this and that , bla bla bla

2014-05-20 13:56:48
more bla bla bla

2014-05-20 14:03:15
once more bla bla bla
regex oracle date-arithmetic
2个回答
0
投票

unix时间戳只是自unix时代以来的秒数(有时是毫秒,但你的是十位数,所以秒)。因此,在使用正则表达式提取unix时间戳之后,我们可以通过使用这样的简单日期算法将它们很容易地转换为常规日期:

select date '1970-01-01' 
       + to_number(
                regexp_replace(HISTODETAILS, '(\d{10})' || chr(4), '\1')
                  ) /86400) 
from your_table
/

要获得您想要的格式,您需要应用适当的掩码,例如

alter session set nls_date_format = 'yyyy-dd-mm hh24:mi:ss'
/

-1
投票

如果您打算使用函数来创建值,我会将regexp_replace也放在该函数中。

select histodetails, myfunction(histodetails) 
from   table
where  ...

和功能

create or replace myfunction(p_histodetails)
return varchar2
as
  v_value varchar2(30); -- or whatever
begin
  -- create stuff to replace with
  v_value := '' -- TODO
  -- return the regexp
  return regexp_replace(p_histodetails,'(\d{10}' || chr(4) ||')', v_value)
end
© www.soinside.com 2019 - 2024. All rights reserved.