SQL 服务器 LIKE 运算符不匹配包含方括号的值
SQL Server LIKE operator not matching values containing square brackets
我的数据库中有一个 table,我在其中插入了所有 table 个名称。
例如我有 table name [test].[TestTable] 并且当我 运行 这个查询
select * from Tables where Name like N'%[test].[TestTable]%'
它 return 没有任何值,但此查询有效:
select * from Tables where Name like N'%[TestTable]%'
谁能解释一下为什么?
你必须转义左方括号:
SELECT * FROM Tables WHERE Name LIKE N'%[[]test].[[]TestTable]%'
-----------------------------------------^ ^
--------------------------------------------------+
当你使用运算符LIKE,并在条件中添加[]时,它有另一个含义。 LIKE "reads" 指定范围内的任意单个字符 ([a-f]) 或集合 ([abcdef]).
尝试使用:像 N'%TestTable%'
我的数据库中有一个 table,我在其中插入了所有 table 个名称。 例如我有 table name [test].[TestTable] 并且当我 运行 这个查询
select * from Tables where Name like N'%[test].[TestTable]%'
它 return 没有任何值,但此查询有效:
select * from Tables where Name like N'%[TestTable]%'
谁能解释一下为什么?
你必须转义左方括号:
SELECT * FROM Tables WHERE Name LIKE N'%[[]test].[[]TestTable]%'
-----------------------------------------^ ^
--------------------------------------------------+
当你使用运算符LIKE,并在条件中添加[]时,它有另一个含义。 LIKE "reads" 指定范围内的任意单个字符 ([a-f]) 或集合 ([abcdef]).
尝试使用:像 N'%TestTable%'