[SOLVED] Get everything before and after a character

Issue

I have 2 rows that contain strings like this :

AX/Aland Island No.5/7865.43/5212.62

KR/Republic of Korea/4-08-2021/8-09-2021

I want everything after two first slashes ‘/’ and before the last slash ‘/’.

I want my result to be:

20142.87
19-02-2022

I know that we can use functions like substring and charindex but I still don’t understand how the syntax is. Please help me. Thank you

Solution

If string between the first two slashes will not start with an integer whatsoever and the string after the first 2 slashes is always going to start with an integer then below is the solution.

declare @tbl table(string varchar(200))

insert into @tbl
values('AX/Aland Island No.5/20142.87/20542.87')
,('KR/Republic of Korea/19-02-2022/19-03-2022')

select SUBSTRING(string,PATINDEX('%[/][0-9]%',string)+1,
CHARINDEX('/',SUBSTRING(string,PATINDEX('%[/][0-9]%',string)+1,len(string)))-1)
from
@tbl

Answered By – Coder1991

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published.