Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL: get a substring using a delimeter

I have column which has – as delimeter and i want to get substring between first delimeter and second delimeter

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

Output

DEF
JAFF

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading