Can someone please help me with trimming the below sample texts in SQL Server? I am looking to get the data between the two '-'
(some rows do not have a '-'
).
AB DCE
CM-AB DCE -228
ABC
CM-AB DCE-214
CJ-ABC-228
The output expected is:
AB DCE
AB DCE
ABC
AB DCE
ABC
I tried:
select substring(mycol, 4, charindex('-', mycol, 12) )
But the results are not accurate.
seems like the question has been answered here
https://www.webcodeexpert.com/2016/08/sql-server-query-to-get-string-between.html
SELECT SUBSTRING(txt,CHARINDEX('-',txt)+1,
(((LEN(txt))-CHARINDEX('-',REVERSE(txt)))-CHARINDEX('-',txt)))
AS Result FROM tab