像sql server 2008中的全长字符串操作符

use*_*270 2 sql-server-2008

在下面需要帮助.

我正在使用sql seerver 2008并有一个查询,其中我使用像运算符.当我使用字符串的一部分然后它的工作正常但是当我在类似运算符数据库中使用完整的字符串时不填充任何结果.

例如.我有表EMp包含描述列.如果descreption列包含

Description
-------------------------------------------------
'John joined on Wednesday/CELL PHONE [1234567890]'
Run Code Online (Sandbox Code Playgroud)

当我在写查询

select * from EMp where 
description like '%CELL%'
Run Code Online (Sandbox Code Playgroud)

它工作正常但是当我写我的查询时

select * from EMp where 
description like '%John joined on Wednesday/CELL PHONE [1234567890]%'
Run Code Online (Sandbox Code Playgroud)

它没有返回任何价值.

这是否意味着like运算符只能处理字符串的一部分而不是完整的字符串.我也试过LTRIM和RTRIM只是为了确保空间不是问题,但它不起作用.

谢谢

Tim*_*ora 5

请记住,LIKE除了通配符外,还支持一组有限的模式匹配%.其中一种模式包括用于匹配范围的括号.

请参阅:http://msdn.microsoft.com/en-us/library/ms179859.aspx

查询中的括号将使其搜索"指定范围内的任何单个字符([af])或设置([abcdef])."

description like '%John joined on Wednesday/CELL PHONE [1234567890]%'
Run Code Online (Sandbox Code Playgroud)

因此,您的查询要求SQL Server在集合[1234567890]中查找字符.

如果您阅读MSDN文档,它提供了使用通配符作为文字的指南.这是一个小例子:

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]%';
Run Code Online (Sandbox Code Playgroud)

请注意,如果要搜索具有更复杂模式的较大字符串,则全文索引可能更有用.所有版本的SQL Server 2008(甚至是Express)都支持它.

  • +1,并解决您需要使用额外支架逃离支架的问题.`描述像'约翰周三加入/手机[[1234567890]%'` (2认同)