Question

I have searched if there is a reason why tsql contains wouldnt allow "or not" or "not" as unary operator.

On the microsoft site it just says "OR NOT operator is not allowed" CONTAINS (Transact-SQL)

I can simply put the unary not in outside of contains to be able to do that right?

SELECT * FROM Person 
WHERE NOT CONTAINS(*, 'Steve')

and the same with "or not"

SELECT * FROM Person 
WHERE CONTAINS(FirstName, 'Peter') OR NOT CONTAINS(LastName, 'Smith')

Is it problematic to do queries as in the two examples?

Thanks for your help
Manuel

Was it helpful?

Solution

Your examples should work.

An expression like CONTAINS( Description, 'NOT "Mountain" ') is probably not allowed because it is equivalent (we can rewrite it) to NOT CONTAINS( Description, 'Mountain').

Similarly, an expression like CONTAINS( Description, '"River" OR NOT "Mountain" ') is equivalent to CONTAINS( Description, 'River') OR NOT CONTAINS( Description, 'Mountain').

OTHER TIPS

Do you have some specific reason to do Full Text search in your situation?

In your example you might want simplify query to the following:

SELECT * FROM Person
WHERE FirstName LIKE '%Peter%' OR LastName NOT LIKE '%Smith%'

If you need to use Full Text search, you could change query to something like this:

SELECT * FROM Person 
WHERE CONTAINS(FirstName, 'Peter')

EXCEPT

SELECT * FROM Person 
WHERE CONTAINS(LastName, 'Smith')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top