SQL 选择列以 \ 开头的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9141521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL select where column begins with \
提问by Korhan Ozturk
I'm trying to find all the data in ColumnX
where the Data begins with a \
.
Is like '\%'
what I'm looking for? But the \
has to be at the beginning.
我试图ColumnX
在 Data 以\
.
是like '\%'
我要找的吗?但\
必须在开始。
回答by Erwin Brandstetter
The backslash \
can have special meaning in LIKE
patterns. It's the default ESCAPE
character in PostgreSQLor MySQL- but not in Oracleor SQL Server. (Can be modified with an added ESCAPE
clause in subtly varying ways, follow link for your RDBMS.)
反斜杠\
在LIKE
模式中可以具有特殊含义。它是PostgreSQL或MySQL 中的默认ESCAPE
字符- 但不是Oracle或SQL Server。(可以通过添加的子句以细微变化的方式进行修改,请按照您的 RDBMS 的链接进行修改。)ESCAPE
In addition to that, PostgreSQLused to interpret POSIX-style escapes in strings (a.k.a. "C escape syntax") before version 9.1 and MySQLstill does in version 8.0). There you have to double \
to \\
to get an ordinary backslash.
除此之外,PostgreSQL用于在 9.1 版之前解释字符串中的 POSIX 样式转义(又名“C 转义语法”),而MySQL在 8.0 版中仍然如此)。在那里,你必须仔细\
来\\
获得一个普通的反斜杠。
According to standard SQL, \
is nota meta character in strings. PostgreSQL eventually switched to standard behavior and introduced a special escape-string-syntax with E''
and the config settings standard_conforming_strings
and escape_string_warning
to still allow escaped string when needed.
根据标准SQL,\
是不是在串的元字符。PostgreSQL 最终切换到标准行为并引入了特殊的转义字符串语法E''
和配置设置,standard_conforming_strings
并escape_string_warning
在需要时仍然允许转义字符串。
Since Postgres 9.1standard_conforming_strings = on
by default. So you write:
默认情况下从Postgres 9.1 开始standard_conforming_strings = on
。所以你写:
... col LIKE '\%' -- double once to get 1 literal backslash in LIKE pattern
Else you have to write:
否则你必须写:
... col LIKE E'\\%' -- double twice to also disable special meaning as escape char
On top of that, \
also has special meaning in many client programs and programming languages. If strings get interpreted before they even reach the database engine, you may have to double the \
another time (or escape it in some other fashion). See this related question, for instance.
最重要的是,\
在许多客户端程序和编程语言中也具有特殊含义。如果字符串在它们到达数据库引擎之前就被解释了,你可能不得\
不再重复一次(或以其他方式转义它)。例如,请参阅此相关问题。
回答by Korhan Ozturk
Indeed '\%' is what you need. The following statement brings all the data where columnX starts with '\'.
确实 '\%' 正是您所需要的。以下语句包含 columnX 以 '\' 开头的所有数据。
select * from table_name where columnX like '\%'
回答by Fahim Parkar
try this...
尝试这个...
select * from myTable where ColumnX like '\%'
select * from myTable where ColumnX like '\%'
Good Luck!!!
祝你好运!!!
回答by Royi Namir
this is what you need
这就是你所需要的