-1

I have multiple records like this: GUID-155_188.PNG

I need to take the string 155, of course this number is variable so there could be 1, 2, 3000, etc.

But the syntax is always the same, is between a - and a _.

So, how can I extract that part between these two characters?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
gaccerboni
  • 57
  • 7
  • 1
    Check out [this answer](https://stackoverflow.com/a/17975453/2516576) – Jesse Lawson Mar 13 '20 at 18:35
  • 3
    Does this answer your question? [A SQL Query to select a string between two known strings](https://stackoverflow.com/questions/18362260/a-sql-query-to-select-a-string-between-two-known-strings) – demo Mar 13 '20 at 18:36
  • is not what i was expecting but i'm satisfied – gaccerboni Mar 13 '20 at 18:52

2 Answers2

0

Try this out:

DECLARE @g varchar(1000) = 'GUID-155_188.PNG'
SELECT LEFT(RIGHT(@g, CHARINDEX('-', REVERSE(@g), 0) - 1), CHARINDEX('_', (RIGHT(@g, CHARINDEX('-', REVERSE(@g), 0) - 1)), 0) - 1)
soccer7
  • 3,547
  • 3
  • 29
  • 50
0

Try this:

DECLARE @string VARCHAR(100) = 'GUID-15_188.PNG'
SELECT SUBSTRING(@string, CHARINDEX('-', @string) + 1, CHARINDEX('_', @string) - CHARINDEX('-', @string) - 1)
Nayanish Damania
  • 542
  • 5
  • 13