Search code examples
sqlsql-serversubstringcharindex

Split a string to only use the middle part in SQL


I have a string

ABC - ABCDEFGHIJK - 05/07/2016

I want to only use the ABCDEFGHIJK section and remove the first and third parts of the string.

I have tried using SUBSTRING with CHARINDEX, but was only able to remove the first part of the string.

Anyone help with this?


Solution

  • You can use SUBSTRING_INDEX() and TRIM() for spaces :

    SELECT TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(string_col,'-',2),'-',-1)) AS Strig_Col
    FROM YourTable;