Here is a problem that I have encountered repeatedly when playing with Stack Exchange Data Explorer , which is based on T-SQL:
How to search for a string, except when it occurs as a substring of any other string?
For example, how can I select all the entries in the MyTable table, where the MyCol column contains the row foo , but ignores any foo that are part of the foobar row?
A quick and dirty attempt would look something like this:
SELECT * FROM MyTable WHERE MyCol LIKE '%foo%' AND MyCol NOT LIKE '%foobar%'
but obviously this will not match, for example. MyCol = 'not all foos are foobars' , which I want to map.
One solution that I came up with is to replace all occurrences of foobar with some dummy marker (which is not a substring of foo ), and then check all the remaining foo s, as in:
SELECT * FROM MyTable WHERE REPLACE(MyCol, 'foobar', 'X') LIKE '%foo%'
This works, but I suspect that it is not very efficient, since it has to run REPLACE() for every record in the table. (For SEDE, this will usually be a Posts table, which currently has about 30 million rows.) Are there any better ways to do this?
(FWIW, the real use case that raised this question was looking for SO messages with image URLs that use the http:// scheme prefix but not point to i.stack.imgur.com host.)