Let’s suppose you have a basic hierarchy represented in a string. e.g.
You are only interested in retrieving the first part of the string/hierarchy, you could use the following functions:
- CHARINDEX - Find the position of a character
- LEFT return ‘n’ numbers of characters of the left hand side of a string
Here is some sample code to show how this would work:
DECLARE @string VARCHAR(200);
We are looking for the first occurrence of ‘|’ and then use the left function to pull everything before that character.
We have added a ‘|’ to the end of the string, in case there is no ‘|’. This will stop an error on the LEFT function trying to read to -1 places.