我有查询删除双空格并将其转换为单空格。
UPDATE tablename SET name=trim(regexp_replace(name,'\s\s+',' ', 'g'));
它给出错误:
WARNING: nonstandard use of escape in a string literal
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
您正在使用以下设置运行旧版本的 Postgresescape_string_warning = on
(默认)和standard_conforming_strings = off
(过时了!,默认为on
从 Postgres 9.1 开始)。手册: https://www.postgresql.org/docs/current/static/runtime-config-compatible.html#GUC-ESCAPE-STRING-WARNING
escape_string_warning
(boolean
)
打开时,如果反斜杠 (\
) 出现在普通的
字符串字面量 ('...'
语法)和standard_conforming_strings
is off
。
默认为on
. (...)
只是修复语法并摆脱WARNING
:
trim(regexp_replace(name, E'\\s\\s+', ' ', 'g'))
正确的解决方案:升级到最新版本的 Postgres,或将过时的设置修复为standard_conforming_strings =
on
.
在现代 Postgres 中,您的表达式按原样有效。
准确地说,\s是类的简写[[:space:]] https://www.postgresql.org/docs/current/static/functions-matching.html#POSIX-CLASS-SHORTHAND-ESCAPES-TABLE, 包括any有点儿空白 https://en.wikipedia.org/wiki/Whitespace_character(包括 tab、nbsp 等)。您的表达式将任何两个或多个空格字符的字符串替换为单个空格字符。符合您的描述的表达式是:
trim(regexp_replace(name,' +', ' ', 'g'))
...无论版本和以上设置如何,它都可以工作。
Related:
在 PostgreSQL 中插入带单引号的文本 https://stackoverflow.com/questions/12316953/insert-text-with-single-quotes-in-postgresql/12320729#12320729
将 varchar 字符串排序为数字 https://stackoverflow.com/questions/8502505/order-varchar-string-as-numeric/8502570#8502570
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)