postgresql 7.4 是否有 regexp_replace 等价物?

Is there a regexp_replace equivalent for postgresql 7.4?

我正在尝试使用 SELECT regexp_replace(m.*, '[\n\r]+', ' ', 'g') 从我的字段中删除回车符 returns 和新行以从我的 table 生成 CSV;但是,看起来我的 postgresql 版本 (7.4.27) 不支持该功能。

function regexp_replace(members, "unknown", "unknown", "unknown") does not exist

我也试过这样做:

SELECT replace(replace(m.*, '\r', ''), '\n', '')

function replace(members, "unknown", "unknown") does not exist

No function matches the given name and argument types. You may need to add explicit type casts.

或者这样:

SELECT replace(replace(m.*, chr(13), ''), chr(10), '')

function replace(members, text, "unknown") does not exist

仍然出现类似的错误。

如何使用其他功能或解决方案来实现?

m.* 你把它放在哪里没有意义。它会像这样工作:

SELECT replace(replace(<b>m.some_column</b>, chr(13), ''), chr(10), '')
FROM   tbl m;

但这只会完全删除所有 "linefeed" 和 "carriage return" 字符,而不是像原来那样用单个 space 字符替换仅由这些字符组成的每个字符串。如果那是你想要的,使用 translate() 单个字符替换更简单、更便宜 - 也可用于 ancient pg 7.4:

SELECT translate(some_column, chr(13) || chr(10), '');

要实现原始 regexp_replace() 的功能(只是没有无意义的 m.*),请识别不在字符串中的单个字符并将其用作垫脚石。说:°不弹出,则:

SELECT replace(replace(replace(
        translate(some_column, chr(13) || chr(10), '°')  -- replace with dummy
      , '°°', '°')  -- consolidate to single dummy
      , '°°', '°')  -- repeat as many times as necessary
      , '°', ' ');  -- replace dummy with space

看起来很别扭,而且不完美:连续换行太多而失败。但它可能仍然比 regexp_replace() 快,即使在现代 Postgres 中也是如此,因为正则表达式要昂贵得多。再一次,性能在这里可能不是问题。

升级到现代 Postgres,你不需要这个。