Reputation: 785
Need help in below.
I am using sql seerver 2008 and have a query in which i am using like operator. when i am using a part of string then its working fine however when i am using complete string in like operator database is not populating any results.
for example. i have table EMp which containd description column. if descreption column contains
Description
-------------------------------------------------
'John joined on Wednesday/CELL PHONE [1234567890]'
when i am writing query
select * from EMp where
description like '%CELL%'
its working fine however when i am writing my query as
select * from EMp where
description like '%John joined on Wednesday/CELL PHONE [1234567890]%'
its not returning any value.
does it mean that like operator works only part of string and not on full string. I have also tried LTRIM and RTRIM just to make sure that space is not a problem however its not working.
Thanks
Upvotes: 2
Views: 3885
Reputation: 54377
Keep in mind that LIKE
supports a limited set of pattern matches in addition to the wildcard %
. One of those patterns includes brackets for matching a range.
See: http://msdn.microsoft.com/en-us/library/ms179859.aspx
The brackets in your query will cause it to search for "Any single character within the specified range ([a-f]) or set ([abcdef])."
description like '%John joined on Wednesday/CELL PHONE [1234567890]%'
Thus, your query is asking for SQL Server to find a character within the set [1234567890].
If you read through the MSDN documentation, it provides guidelines for using wildcard characters as literals. Here's a little example:
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]%';
Note that full text indexing may be more useful if you want to search larger strings with more complex patterns. It is supported in all editions of SQL Server 2008 (even Express).
Upvotes: 5