Немного более простой способ в соответствии с SUBSTRING, PATHINDEX or CHARINDEX
DECLARE @Text VARCHAR(MAX) = '<Date & Time Stamp>- User 1. What has caused the issue ? There was a script which mistakenly included and executed during deployment which caused
loutage 2. How was the issue resolved ? The Negative Impact was resolved by reverting back the
I changes which were deployed and getting back to the previous state.
3. What action are being taken to ensure similar negative impact will not occur again in future ?
Considering a humar error, scripts are being reviewed more diligently over the future release and
we have advised developments teams to keep a track of what is being pushed to make sure it does
'
DECLARE @RevisedText VARCHAR(MAX) = @Text
SET @RevisedText = REPLACE(@RevisedText, '<Date & Time Stamp>- User 1. What has caused the issue ?', '|{1}')
SET @RevisedText = REPLACE(@RevisedText, '2. How was the issue resolved ?', '|{2}')
SET @RevisedText = REPLACE(@RevisedText, '3. What action are being taken to ensure similar negative impact will not occur again in future ?', '|{3}')
SELECT
Col_1 = (SELECT LTRIM(RTRIM(REPLACE(value, '{1}', ''))) FROM string_split(@RevisedText, '|') WHERE value LIKE '{1}%'),
Col_2 = (SELECT LTRIM(RTRIM(REPLACE(value, '{2}', ''))) FROM string_split(@RevisedText, '|') WHERE value LIKE '{2}%'),
Col_3 = (SELECT LTRIM(RTRIM(REPLACE(value, '{3}', ''))) FROM string_split(@RevisedText, '|') WHERE value LIKE '{3}%')
Примечание. Если у вас есть SQL Сервер с уровнем совместимости 130, вы можете использовать UDF
вместо собственного split_string
CREATE FUNCTION string_split (
@String NVARCHAR(MAX)
, @Delimiter CHAR(1)
)
RETURNS @Table TABLE (StrCol NVARCHAR(MAX))
AS
BEGIN
DECLARE @Delimiter1 CHAR(3) = '%' + @Delimiter + '%'
WHILE PATINDEX(@Delimiter1 , @String) <> 0
BEGIN
INSERT INTO @Table VALUES ( LEFT(@String,PATINDEX(@Delimiter1 , @String)-1) )
SET @String = STUFF(@String, 1, PATINDEX(@Delimiter1 , @String), '')
END
INSERT INTO @Table VALUES (@String )
RETURN
END