1

I am trying to extract a string between two characters. The length could change for any of the numbers. The values I am trying to extract is only e.g. 0001A, 0002BB, 0003C etc.

Using select SUBSTRING(ordtxt,7,4) as ordtxt, I would be able to only extract x amount of characters from the left which isn't really something I am looking for.

100/0/0001A/001
101/000/0002BB/001
102/00/0003C/0001

Thank you for any help.

VQB
  • 57
  • 7

1 Answers1

6

You could use a simple XML approach, which would isolate values from a delimited string

 SELECT
   convert(xml,'<x>'+replace(YourColumn,'/','</x><x>')+'</x>').value('/x[3]','varchar(100)')
 FROM YourTable

The solution breaks down the string into positions denoted by a numeric index, simply select the required element you need.

For example if you need 0001, 0002, 0003 then use a position of 3 in the function above.

The syntax is based on SQL Server

forpas
  • 145,388
  • 9
  • 31
  • 69
jimmy8ball
  • 716
  • 5
  • 15