需要以下帮助。
我正在使用 sql seerver 2008,并且有一个查询,其中我使用 like 运算符。 当我使用字符串的一部分时,它工作正常,但是当我在类似运算符数据库中使用完整字符串时,不会填充任何结果。
例如。 我有表 EMP,其中包含描述列。 如果描述列包含Description
-------------------------------------------------
'John joined on Wednesday/CELL PHONE [1234567890]'
当我编写查询时
select * from EMp where
description like '%CELL%'
工作正常 但是当我将查询写为
select * from EMp where
description like '%John joined on Wednesday/CELL PHONE [1234567890]%'
它没有返回任何值。
这是否意味着 Like 运算符仅适用于字符串的一部分,而不适用于整个字符串。 我还尝试了 LTRIM 和 RTRIM,只是为了确保空间不是问题,但它不起作用。
谢谢
最佳答案
请记住,除了通配符 %
之外,LIKE
还支持一组有限的模式匹配。这些模式之一包括用于匹配范围的括号。
参见:http://msdn.microsoft.com/en-us/library/ms179859.aspx
查询中的方括号将导致其搜索“指定范围 ([a-f]) 或集合 ([abcdef]) 内的任何单个字符。”
description like '%John joined on Wednesday/CELL PHONE [1234567890]%'
因此,您的查询要求 SQL Server 在集合 [1234567890] 中查找字符。
如果您通读 MSDN documentation ,它提供了使用通配符作为文字的指南。这是一个小例子:
DECLARE @table TABLE ( SomeText VARCHAR( 100 ) );
INSERT @table ( SomeText ) VALUES ( 'here is a string with [brackets]' );
-- matches with wildcards on both sides of the pattern
SELECT * FROM @table WHERE SomeText LIKE '%[brackets]%';
-- won't match
SELECT * FROM @table WHERE SomeText LIKE '%here is a string with [brackets]%';
-- matches, because expression is escaped
SELECT * FROM @table WHERE SomeText LIKE '%here is a string with [[brackets]%';
-- a confusing (but valid) escape sequence AND a wildcard
SELECT * FROM @table WHERE SomeText LIKE '%here is a string with [[][a-z]rackets]%';
请注意,如果您想搜索具有更复杂模式的更大字符串,全文索引可能会更有用。 SQL Server 2008 的所有版本(甚至 Express)都支持它。
关于sql-server-2008 - sql server 2008 中全长字符串的 Like 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14314242/