Sample Header Ad - 728x90

At finding a substring, find the end position as well

5 votes
2 answers
1799 views
A LIKE clause can test if a string occurs in another and the CHARINDEX function can give the start position of the first match. In my case, I'm interested in the *end position* though, which is, due to the intricacies of collations, not derivable from the start location. For example, in a German collation (German_PhoneBook_100_CI_AS_SC_UTF8), - occurs in 'Häger' at position 1 and ends at position 2 and - occurs in 'Haeger' at position 1 and ends at position 3. The problem this is for is to mark the matching part of a search result text for the users benefit. I've been thinking about reversing the strings, but then I still can get only the first match with CHARINDEX and in that reversed case I'd need the last. Any ideas anyone?
Asked by John (775 rep)
Sep 8, 2022, 03:54 PM
Last activity: Sep 29, 2022, 01:21 AM