Returning selection portions of values in a resultset.
When searching for specific values within a field, returning portions surrounding the search criteria may be very helpful for providing the user a context in which the value in being used.
In this example, I will illustrate searching within a “stories” table that contain strings about someone walking down the street.
However, instead of just returning the PKs of matching records, I want to show the user “who” walked each dog.
In this code example, I am able to use a combination of substring() and charindex() string function to both find matching records and return a portion before the matching characters – “who” walked down the street.
As you can see, specifying to begin ‘6’ characters before “walked” and return ‘5’ of those characters, I can see in each “story” who walked down the street.