Among the many options is to create a simple function.
Can keep your code cleaner.
Gives the ability to handle errors if the start or end marker/string is not present.
This function also allows for trimming leading or trailing whitespace as an option.
SELECT dbo.GetStringBetweenMarkers('123456789', '234', '78', 0, 1)
Yields:
56
--Code to create the function
USE [xxxx_YourDB_xxxx]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[GetStringBetweenMarkers] (@FullString varchar(max), @StartMarker varchar(500), @EndMarker varchar(500), @TrimLRWhiteSpace bit, @ReportErrorInResult bit)
RETURNS varchar(max)
AS
BEGIN
--Purpose is to simply return the string between 2 string markers. ew 2022-11-06
--Will perform a LTRIM and RTRIM if @TrimLRWhiteSpace = 1
--Will report errors of either marker not being found in the RETURNed string if @ReportErrorInResult = 1.
-- When @ReportErrorInResult = 0, if the start marker isn't found, will return everything from the start of the @FullString to the left of the end marker.
-- When @ReportErrorInResult = 0, if the end marker isn't found, SQL will return an error of "Invalid length parameter passed to the LEFT or SUBSTRING function."
DECLARE @ReturnString VARCHAR(max) = ''
DECLARE @StartOfStartMarker INT = CHARINDEX(@StartMarker, @FullString)
DECLARE @StartOfTarget INT = CHARINDEX(@StartMarker, @FullString) + LEN(@StartMarker)
DECLARE @EndOfTarget INT = CHARINDEX(@EndMarker, @FullString, @StartOfTarget)
--If a marker wasn't found, put that into the
IF @ReportErrorInResult = 1
BEGIN
IF @EndOfTarget = 0 SET @ReturnString = '[ERROR: EndMarker not found.]'
IF @StartOfStartMarker = 0 SET @ReturnString = '[ERROR: StartMarker not found.]'
IF @StartOfStartMarker = 0 AND @EndOfTarget = 0 SET @ReturnString = '[ERROR: Both StartMarker and EndMarker not found.]'
END
--If not reporting errors, and start marker not found (i.e. CHARINDEX = 0) we would start our string at the LEN(@StartMarker).
-- This would give an odd result. Best to just provide from 0, i.e. the start of the @FullString.
IF @ReportErrorInResult = 0 AND @StartOfStartMarker = 0 SET @StartOfTarget = 0
--Main action
IF @ReturnString = '' SET @ReturnString = SUBSTRING(@FullString, @StartOfTarget, @EndOfTarget - @StartOfTarget)
IF @TrimLRWhiteSpace = 1 SET @ReturnString = LTRIM(RTRIM(@ReturnString))
RETURN @ReturnString
--Examples
-- SELECT '>' + dbo.GetStringBetweenMarkers('123456789','234','78',0,1) + '<' AS 'Result-Returns what is in between markers w/ white space'
-- SELECT '>' + dbo.GetStringBetweenMarkers('1234 56 789','234','78',0,1) + '<' AS 'Result-Without trimming white space'
-- SELECT '>' + dbo.GetStringBetweenMarkers('1234 56 789','234','78',1,1) + '<' AS 'Result-Will trim white space with a @TrimLRWhiteSpace = 1'
-- SELECT '>' + dbo.GetStringBetweenMarkers('abcdefgh','ABC','FG',0,1) + '<' AS 'Result-Not Case Sensitive'
-- SELECT '>' + dbo.GetStringBetweenMarkers('abc_de_fgh','_','_',0,1) + '<' AS 'Result-Using the same marker for start and end'
--Errors are returned if start or end marker are not found
-- SELECT '>' + dbo.GetStringBetweenMarkers('1234 56789','zz','78',0,1) + '<' AS 'Result-Start not found'
-- SELECT '>' + dbo.GetStringBetweenMarkers('1234 56789','234','zz',0,1) + '<' AS 'Result-End not found'
-- SELECT '>' + dbo.GetStringBetweenMarkers('1234 56789','zz','zz',0,1) + '<' AS 'Result-Niether found'
--If @ReportErrorInResult = 0
-- SELECT '>' + dbo.GetStringBetweenMarkers('123456789','zz','78',0,0) + '<' AS 'Result-Start not found-Returns from the start of the @FullString'
-- SELECT '>' + dbo.GetStringBetweenMarkers('123456789','34','zz',0,0) + '<' AS 'Result-End found-should get "Invalid length parameter passed to the LEFT or SUBSTRING function."'
END
GO