Extract (Get) string from String in SQL Server

Last Reply 5 days ago By dharmendr

Posted 5 days ago

I want a specific string from main string like "18 - Dhanvantari Hospital-Koldam, Himachal Pradesh".

And i want only "Dhanvantari Hospital-Koldam"

I am looking for string between -(dash) and ,(comma) 

Any idea how to do that in SQL or ASP.Net

You are viewing reply posted by: dharmendr 5 days ago.
Posted 5 days ago Modified on 4 days ago

Check below query.

DECLARE @string VARCHAR(100) = '18 - Dhanvantari Hospital-Koldam, Himachal Pradesh'
SELECT SUBSTRING(@string,CHARINDEX('-',@string)+1,CHARINDEX(',',@string)-CHARINDEX('-',@string)-1)

SET @string = '7 - Stabilization Center-Gadarwara, Madhya Pradesh'
SELECT SUBSTRING(@string,CHARINDEX('-',@string)+1,CHARINDEX(',',@string)-CHARINDEX('-',@string)-1)

Output

Dhanvantari Hospital-Koldam

Stabilization Center-Gadarwara