1

I have column which has - as delimiter and I want to get substring between the first and second delimiter:

ABC-DEF-FG
ABCD-JAFF-UIOU-TYU

Output

DEF
JAFF

I have tried substring and charIndex but not getting exactly what I wanted

select * SUBSTRING(column, CHARINDEX('-', column)+1, len(column)) 
from table

select * SUBSTRING(column, CHARINDEX('-', column)+1, charindex('-', column, (charindex('-', column, 1)))) 
from table

select * SUBSTRING(column, CHARINDEX('-', column)+1, charindex('-', column, (charindex('-', column, 1))+1)) 
from table
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user09
  • 920
  • 2
  • 12
  • 38
  • Does this answer your question? [Turning a Comma Separated string into individual rows](https://stackoverflow.com/questions/5493510/turning-a-comma-separated-string-into-individual-rows) – Stoff Jul 08 '22 at 21:19
  • Failed to execute query. Error: 'STRING_SPLIT' is not a recognized built-in function name. – user09 Jul 08 '22 at 21:29
  • @user09 What version on you using SELECT @@version – John Cappelletti Jul 08 '22 at 21:31
  • Microsoft SQL Azure (RTM) - 12.0.2000.8 May 12 2022 23:11:24 Copyright (C) 2022 Microsoft Corporation – user09 Jul 08 '22 at 21:36

1 Answers1

3

With a bit of JSON

Example

Select A.[column] 
      ,Pos2  = JSON_VALUE(S,'$[1]')
From  YourTable A
Cross Apply ( values ( '["'+replace(string_escape([column],'json'),'-','","')+'"]' ) ) B(S)

Results

column              Pos2
ABC-DEF-FG          DEF
ABCD-JAFF-UIOU-TYU  JAFF
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66