tags:

views:

104

answers:

3

I have some table 'TableName' like Id,Name:

1 | something
2 | _something
3 | something like that
4 | some_thing
5 | ...

I whant to get all rows from this table where name containes 'some'. I have 2 ways:

SELECT * FROM TableName WHERE Name like '%some%'

Result is table :

1 | something
2 | _something
3 | something like that
4 | some_thing

But if I use CONTAINS function

SELECT * FROM TableName WHERE CONTAINS(Name,'"*some*"')

I get only

1 | something
3 | something like that

What should I do to make CONTAINS function work properly?

A: 

Hi,

http://doc.ddart.net/mssql/sql70/ca-co_15.htm

If you read this article you will see that * means prefix this means that word must start whit this but like means the word contains key phrase

Best Regards, Iordan

IordanTanev
I know what prefix * stands for, I use it to get strings like 'something like that' or 'this is something'. I'm interesting in how to get string with underlinings using CONTAINS function.
Kate
then you must add one more CONTAINS function with parent with underline SELECT * FROM TableName WHERE CONTAINS(Name,'"*some*"') OR CONTAINS(Name,'"*_some*"')
IordanTanev
If I use it I can get strings like 'something', '_something', but can't get 'some_thing'. I want get all string with word 'some', so you way is not panacea.
Kate
the do it with like operatot or just add OR CONTAINS(Name,'"*some_*"') to previous expression i prefer using like
IordanTanev
Thank you, I try it but CONTAINS(Name,'"*_some*"') don't match 'this_something' string, only '_something'. On the one hand CONTAINS work faster then Like statement, on the other CONTAINS doesn't match string I wish, so you are right I better use Like.
Kate
I read some where on the internet that if the column you filter with like has index on it and the like filter has % only on right then the index works and like is faster. CONTAINS(Name,'"_some"') OR like 'some%' might be faster then just like '%some%' but you have to test this.
IordanTanev
Thank you answer, but I need '%some%' statement.
Kate
A: 

Try this:

SELECT * FROM TableName WHERE CONTAINS(Name,'some')
codaddict
It is too easy. At fist I try it and it is'n work.
Kate
A: 

The last time I looked (admittedly SQL Server 2000) CONTAINS didn't support wildcard matching at the beginning of words, only at the end. Also, you might need to check your noise files to see if the "_" character is being ignored.

Also see

http://stackoverflow.com/questions/3400/how-do-you-get-leading-wildcard-full-text-searches-to-work-in-sql-server

davek