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 How to extract Middle Characters in a string

I have a string that looks like this. I need to extract after the third ‘/’, or to pull out the section called ‘Reports’ from this string.

/WM/Operational/Reports/ReportName

I have tried the following:

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

This gives me the last part or the ReportName

= RIGHT(col,charindex('/',reverse(col),1)-1)

this removes the leading characters after the 3rd ‘/’ character (number of characters are different than above string to hide details)

= RIGHT(col, LEN(col) - 31)

How do I combine this together to remove the first part after the 3rd ‘/’ and remove the reportname?

>Solution :

Assuming the format of your example string has no more than 4 parts you could make use of parsename here:

declare @col varchar(50) = '/WM/Operational/Reports/ReportName';

select ParseName(Replace(stuff(@col, 1, 1, ''), '/', '.'), 2);
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