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.
2条答案
按热度按时间1yjd4xko1#
If you gather the position of your first symbol, and the position of your second symbol with
CHARINDEX
, you can extract what's contained inbetween withSUBSTRING
.Output:
Check the demo here .
wz8daaqr2#
seems like the question has been answered here
https://www.webcodeexpert.com/2016/08/sql-server-query-to-get-string-between.html