August 10, 2020 at 1:07 pm
Hi,
I want to remove square brackets and text from either start of string or end of string . Requirements:
CREATE TABLE #RemoveText ( ID INT,TextName NVARCHAR(100))
INSERT INTO #RemoveText (ID,TextName)
SELECT '1','[Accounts Name] - Transactional Process sessions' UNION
SELECT '2', 'SR1a - Agree on all scope changes [AST]' UNION
SELECT '3','No Change needed with parenthesis (target 98%)' UNION
SELECT '4','ABCD text'
--Desired TextName
SELECT '1','Transactional Process sessions' UNION
SELECT '2', 'SR1a - Agree on all scope changes' UNION
SELECT '3','No Change needed with parenthesis (target 98%)' UNION
SELECT '4','ABCD text'
DROP TABLE #RemoveText ?
August 10, 2020 at 1:35 pm
Here's one way.
SELECT CalcTextName = CASE
WHEN calcs2.Pos3 > 0 THEN
TRIM(RIGHT(rt.TextName, LEN(rt.TextName) - calcs2.Pos3))
WHEN calcs1.Pos1 > 0
AND calcs1.Pos2 > 0 THEN
TRIM(LEFT(rt.TextName, calcs1.Pos1 - 1))
ELSE
rt.TextName
END
FROM #RemoveText rt
CROSS APPLY
(
SELECT Pos1 = CHARINDEX('[', rt.TextName)
,Pos2 = CHARINDEX(']', rt.TextName)
) calcs1
CROSS APPLY
(
SELECT Pos3 = IIF(Pos2 > 0, CHARINDEX('-', rt.TextName, Pos2), 0)
) calcs2;
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
August 26, 2020 at 4:48 pm
Thanks for the solution. It works. How do I change this code for the hyphen part ?
The square bracket will not be followed by hyphen . It could have a space after ] or text could immediately start after ].
August 26, 2020 at 4:50 pm
Here is the code snippet
CREATE TABLE #RemoveText ( ID INT,TextName NVARCHAR(100))
INSERT INTO #RemoveText (ID,TextName)
SELECT '1','[Accounts Name] Transactional Process sessions' UNION
SELECT '2', 'SR1a - Agree on all scope changes [AST]' UNION
SELECT '3','No Change needed with parenthesis (target 98%)' UNION
SELECT '4','ABCD text' UNION
SELECT '5','[Accounts Name]New Transactional Process sessions'
--Desired TextName
SELECT '1','Transactional Process sessions' UNION
SELECT '2', 'SR1a - Agree on all scope changes' UNION
SELECT '3','No Change needed with parenthesis (target 98%)' UNION
SELECT '4','ABCD text' Union
SELECT '5','New Transactional Process sessions'
DROP TABLE #RemoveText ?
August 26, 2020 at 5:15 pm
None of your revised test data matches this requirement:
If the square bracket is found at the beginning of string and is followed by '-' then I want entire text after the '-' character and exclude everything from [] to '-'
So it sounds like your requirement has changed. Or the data is duff. Please clarify.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
August 26, 2020 at 5:36 pm
Yes, requirement has changed . There will be no more hyphen after [] .
August 26, 2020 at 5:39 pm
OK, so the requirement now is "remove any square brackets and any text they may contain", is that correct?
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
August 26, 2020 at 5:43 pm
Yes, that's correct . Thanks.
August 26, 2020 at 5:51 pm
Makes it easier. Try this.
SELECT CalcTextName = CASE
WHEN calcs1.Pos1 > 0
AND calcs1.Pos2 > calcs1.Pos1 THEN
TRIM(STUFF(rt.TextName, calcs1.Pos1, calcs1.Pos2 - calcs1.Pos1 + 1, ''))
ELSE
rt.TextName
END
FROM #RemoveText rt
CROSS APPLY
(
SELECT Pos1 = CHARINDEX('[', rt.TextName)
,Pos2 = CHARINDEX(']', rt.TextName)
) calcs1;
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
August 27, 2020 at 2:00 pm
Works like a charm. Thank you!
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply