views:

1889

answers:

3

In T-SQL, how would you check if a string doesn't contain another string?

I have an nvarchar which could be "Oranges Apples".

I would like to do an update where, for instance, a columm doesn't contain "Apples".

How can this be done?

+6  A: 
WHERE NOT (someColumn LIKE "%Apples%")
Daniel A. White
Why didn't I think of that, I was going for something much more advanced :)
Dofs
+3  A: 

Or alternatively, you could use this:

WHERE CHARINDEX(N'Apples', someColumn) = 0

Not sure which one performs better - you gotta test it! :-)

Marc

UPDATE: the performance seems to be pretty much on a par with the other solution (WHERE someColumn NOT LIKE '%Apples%') - so it's really just a question of your personal preference.

marc_s
Performance is not such a big issue in my case.
Dofs
+2  A: 

Use this as your WHERE condition

WHERE CHARINDEX('Apples', column) = 0
dutch