0

I need to trim string between occurrences of character '-'. string is 'location-servername-DB-table-location-servername-ID' and end result needs to be substring between forth and fifth(last) '-'

example:

DECLARE @String NVARCHAR(max) ='SERVER-NAME1-DB-TABLE-SERVER-NAME2-NUMBER'
SELECT
substring(right(@String, charindex('-', @String, charindex('-', @String)+3)-5), 1, (len(right( @String, charindex('-',  @String, charindex('-',  @String)+1)+1)) -10)) [SUBSCRIBER SERVER NAME]
Thom A
  • 88,727
  • 11
  • 45
  • 75
Srdjan
  • 1
  • 1
  • Does this answer your question? [Using T-SQL, return nth delimited element from a string](https://stackoverflow.com/questions/19449492/using-t-sql-return-nth-delimited-element-from-a-string) – Thom A Jun 03 '21 at 09:14
  • It's a bit of a performance whack, but you can find the last - by searching the REVERSEd string, and taking that index found off of the length – Caius Jard Jun 03 '21 at 09:17

0 Answers0