I am trying to get the string between hyphen (-) which is before (+) upto next colon (:)

Posted on

Question :

03/02/2020, 7:41 pm - +91 9999999999: At 7:10;
03/02/2020, 7:41 pm - + abcd : At 7:10;

Note: The string in between - and : is not of same length in each record. Please, help me with a solution applicable in such case.

Answer :

SELECT txt, 
       SUBSTRING(txt, CHARINDEX('-', txt), CHARINDEX(':', txt, CHARINDEX('-', txt)) - CHARINDEX('-', txt))
FROM test

or

SELECT txt, 
       SUBSTRING(txt, CHARINDEX('- +', txt), CHARINDEX(':', txt, CHARINDEX('- +', txt)) - CHARINDEX('- +', txt))
FROM test

fiddle

Leave a Reply

Your email address will not be published. Required fields are marked *