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?
1. Using String#contains() method. The standard solution to check if a string is a substring of another string is using the String#contains() method. It returns true if the string contains the specified string, false otherwise.
To check if a string contains a substring in Python using the in operator, we simply invoke it on the superstring: fullstring = "StackAbuse" substring = "tack" if substring in fullstring: print("Found!") else: print("Not found!")
You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false . Also note that string positions start at 0, and not 1.
The includes() method returns true if a string contains a specified string. Otherwise it returns false .
WHERE NOT (someColumn LIKE '%Apples%')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With