I’d like to do something that I think is fairly trivial using T-SQL//SQL Server 2008 R2, but I can’t seem to figure out a way.
If I were in Java, C#, C++, whatever, I would do:
- Find position of first occurrance of ‘123’ in string
- Execute substring operation from that position getting next 50 characters
So, in SQL Server, I’d basically like:
- Find all rows where column (X) contains said string (basically a
LIKE clause) - Return 50 characters from that column starting at the said string’s location.
Can I do this somehow? I can cast an XML column to nvarchar(max), do a like operation, and do a substring operation, I don’t know how to get the position of the said string in the column in the first place though.
Sample content requested in comment
CREATE TABLE SampleTable(xmlData xml);
Pretend the value is in one if SampleTable’s xmlData column is as follows. I would like to, for debugging purposes, extract the string from the funny unicode Þ character forward 50 characters (or to the end of the file if that’s less than 50).
<RootNode>
<Row>
<NestedNode1>
some text.
</NestedNode1>
<NestedNode2>
123456
</NestedNode2>
<NestedNode3>
Þ Some crazy name with unicode letters. Þ
</NestedNode3>
</Row>
</RootNode>
Are you looking for CHARINDEX?