I have the following query in which I am searching within the XMLData type column.
I want to return a substring of a node that either starts with the search criteria and X number of characters afterward ending on a full word or a substring which places the search criteria in the middle of the result with X number of characters before and after starting/ending on a new word.
The reason for the 2 notions is that the search criteria might be at the beginning of the node hence X number of characters afterward or if is deep in the text show a before/after characters.
My query seems to be starting with a new word but I cant suss out ending on it, I had a go a reversing the string, doing a patindex on it then doing length of search - patindex but that didn't seem to work.
Thanks
SELECT
P.Title,
SUBSTRING(DATA.value('(/PageContent/Text)[1]', 'VARCHAR(100)'),PATINDEX('%north%',DATA.value('(/PageContent/Text)[1]', 'VARCHAR(100)')) - 20 + PATINDEX('% %',SUBSTRING(DATA.value('(/PageContent/Text)[1]', 'VARCHAR(100)'),PATINDEX('%north%',DATA.value('(/PageContent/Text)[1]', 'VARCHAR(100)')) - 20,999)),999) AS Data
FROM WEBPAGECONTENT W
INNER JOIN WebPage P
ON P.ID = W.PageID
WHERE COALESCE(PATINDEX('%north%',DATA.value('(/PageContent/Text)[1]', 'VARCHAR(100)')),0) > 0