Smithy Smithy - 4 months ago 13
SQL Question

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?

Answer

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

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