警告:非标准使用字符串中的转义字面值 [英] WARNING: nonstandard use of escape in a string literal
问题描述
更新tablename SET name = trim(regexp_replace(名称,'\s\s +','','g'));
它出现错误:
警告:非标准使用字符串中的转义字面值
提示:对转义使用转义字符串语法,例如E'\\\\
'。
旧版本的Postgres,设置 escape_string_warning = on
(默认)和 standard_conforming_strings = off
(已过期! ,自Postgres 9.1起,默认值为
。 手册:
escape_string_warning
(boolean
)
打开时,如果反斜杠(
\
)出现在普通的
string literal('...'
语法)和standard_conforming_strings
是off
。
默认值为。 (...)
要修正语法并摆脱 WARNING
:
trim(regexp_replace(name,E'\\\s\\\s + 'g'))
正确的解决方案:升级到当前版本或者将过时的设置修正为 standard_conforming_strings =
。
在现代Postgres中,您的表达式是有效的。
确切地说, \s
是 [[:space:]]
,其中包括 白色空间(包括tab,等等)。您的表达式使用单个空格字符替换任何两个或多个空格字符串。适合您的描述的表达式将是:
trim(regexp_replace(name,'+','','g' )
...无论版本及以上设置如何,都可以工作。
相关:
-
< a href =https://stackoverflow.com/questions/8502505/order-varchar-string-as-numeric/8502570#8502570>将varchar字符串命名为数字
I have query to remove double space and convert it to single space.
UPDATE tablename SET name=trim(regexp_replace(name,'\s\s+',' ', 'g'));
It gives error:
WARNING: nonstandard use of escape in a string literal HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
You are running an old version of Postgres with the setting escape_string_warning = on
(default) and standard_conforming_strings = off
(outdated!, default is on
since Postgres 9.1). The manual:
escape_string_warning
(boolean
)When on, a warning is issued if a backslash (
\
) appears in an ordinary string literal ('...'
syntax) andstandard_conforming_strings
isoff
. The default ison
. (...)
To just fix the syntax and get rid of the WARNING
:
trim(regexp_replace(name, E'\\s\\s+', ' ', 'g'))
Proper solution: Upgrade to a current version of Postgres, or fix the outdated setting to standard_conforming_strings =
on
.
In modern Postgres, the expression you have is valid as is.
To be precise, \s
is the class shorthand for [[:space:]]
, which includes any kind of white space (incl. tab, nbsp etc.). Your expression replaces any string of two or more white space char with a single space char. The expression to fit your description would be:
trim(regexp_replace(name,' +', ' ', 'g'))
... which works regardless of version and above settings.
Related:
这篇关于警告:非标准使用字符串中的转义字面值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!