WARNING: nonstandard use of escape in a string literal

旧巷老猫 提交于 2019-12-05 14:18:50
Erwin Brandstetter

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) and standard_conforming_strings is off. The default is on. (...)

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!