Eric Brochu
Eric Brochu

Reputation: 109

How to replace anything between 2 specific characters in SQL Server

I'm trying to replace anything between 2 specific characters in a string that contains multiples of those 2 caracters. Take it as a csv format.

Here an example of what i got as data in that field:

0001, ABCD1234;0002, EFGH432562;0003, IJKL1345hsth;...

What I need to retreive from it is all parts before the ',' but not what are between ',' and ';'

I tried with those formula but no success

 SELECT REPLACE(fieldname, ',[A-Z];', ' ') FROM ...
 or
 SELECT REPLACE(fieldname, ',*;', ' ') FROM ...

I need to get

0001 0002 0003

Is there a way to achieve that?

Upvotes: 0

Views: 2571

Answers (2)

LukStorms
LukStorms

Reputation: 29667

You can CROSS APPLY to a STRING_SPLIT that uses STRING_AGG (since Sql Server 2017) to stick the numbers back together.

select id, codes
from your_table
cross apply (
  select string_agg(left(value, patindex('%_,%', value)), ' ') as codes
  from string_split(fieldname, ';') s
  where value like '%_,%'
) ca;
GO
id codes
1 0001 0002 0003

Demo on db<>fiddle here

Extra

Here is a version that also works in Sql Server 2014.
Inspired by the research from @AaronBertrand
The UDF uses a recursive CTE to split the string.
And the FOR XML trick is used to stick the numbers back together.

CREATE FUNCTION dbo.fnString_Split
(
    @str    nvarchar(4000), 
    @delim  nchar(1)
)
RETURNS TABLE 
WITH SCHEMABINDING 
AS 
RETURN
(
  WITH RCTE AS (
    SELECT 
      1 AS ordinal
    , ISNULL(NULLIF(CHARINDEX(@delim, @str),0), LEN(@str)) AS pos
    , LEFT(@str, ISNULL(NULLIF(CHARINDEX(@delim, @str),0)-1, LEN(@str))) AS value
    UNION ALL
    SELECT 
      ordinal+1
    , ISNULL(NULLIF(CHARINDEX(@delim, @str, pos+1), 0), LEN(@str))
    , SUBSTRING(@str, pos+1, ISNULL(NULLIF(CHARINDEX(@delim, @str, pos+1),0)-pos-1, LEN(@str)-pos )) 
    FROM RCTE
    WHERE pos < LEN(@str)
  ) 
  SELECT ordinal, value
  FROM RCTE
);
SELECT id, codes
FROM your_table
CROSS APPLY (
  SELECT RTRIM((
       SELECT LEFT(value, PATINDEX('%_,%', value))+' '
       FROM dbo.fnString_Split(fieldname, ';') AS spl
       WHERE value LIKE '%_,%'
       ORDER BY ordinal
       FOR XML PATH(''), TYPE).value(N'./text()[1]', N'nvarchar(max)')
    ) AS codes
) ca
OPTION (MAXRECURSION 250);
id codes
1 0001 0002 0003

Demo on db<>fiddle here

Alternative version of the UDF (no recursion)

CREATE FUNCTION dbo.fnString_Split
(   
  @str   NVARCHAR(4000),
  @delim NCHAR(1)
)
RETURNS @tbl TABLE (ordinal INT, value NVARCHAR(4000))
WITH SCHEMABINDING
AS
BEGIN
  DECLARE @value NVARCHAR(4000)
        , @pos INT = 0
        , @ordinal INT = 0;
  WHILE (LEN(@str) > 0)
  BEGIN
    SET @ordinal += 1;
    SET @pos = ISNULL(NULLIF(CHARINDEX(@delim, @str),0), LEN(@str)+1);
    SET @value = LEFT(@str, @pos-1);
    SET @str = SUBSTRING(@str, @pos+1, LEN(@str));
    INSERT INTO @tbl (ordinal, value) 
              VALUES (@ordinal, @value);
  END;
  RETURN;
END;

Upvotes: 2

Aaron Bertrand
Aaron Bertrand

Reputation: 280431

If you're on SQL Server 2017 and don't need a guarantee that the order will be maintained, then LukStorms' answer is perfectly adequate.

However, if you:

  • care about an order guarantee; or,
  • are on an older version than 2017 (and can't use STRING_AGG); or,
  • are on an even older version than 2016 or are in an older compatibility level (and can't use STRING_SPLIT):

Here's an ordered split function that can help (it's long and ugly but you only have to create it once):

CREATE FUNCTION dbo.SplitOrdered
(
    @list    nvarchar(max), 
    @delim   nvarchar(10)
)
RETURNS TABLE 
WITH SCHEMABINDING 
AS 
RETURN
(
  WITH w(n) AS (SELECT 0 FROM (VALUES (0),(0),(0),(0)) w(n)),
       k(n) AS (SELECT 0 FROM w a, w b),
       r(n) AS (SELECT 0 FROM k a, k b, k c, k d, k e, k f, k g, k h),
       p(n) AS (SELECT TOP (COALESCE(LEN(@list), 0)) 
                ROW_NUMBER() OVER (ORDER BY @@SPID) -1 FROM r),
       spots(p) AS 
       (
         SELECT n FROM p 
         WHERE (SUBSTRING(@list, n, LEN(@delim + 'x') - 1) LIKE @delim OR n = 0)
       ),
       parts(p,val) AS 
       (
         SELECT p, SUBSTRING(@list, p + LEN(@delim + 'x') - 1, 
           LEAD(p, 1, 2147483647) OVER (ORDER BY p) - p - LEN(@delim)) 
         FROM spots AS s
       )
       SELECT listpos = ROW_NUMBER() OVER (ORDER BY p), 
              Item    = LTRIM(RTRIM(val))
         FROM parts
);

Then the query can become:

;WITH x AS 
(
  SELECT id, listpos, 
    codes = LEFT(Item, COALESCE(NULLIF(CHARINDEX(',', Item),0),1)-1)
  FROM dbo.your_table
  CROSS APPLY dbo.SplitOrdered(fieldname, ';') AS c
)
SELECT id, codes = (
  (SELECT x2.codes + ' '
    FROM x AS x2
     WHERE x2.id = x.id
     ORDER BY x2.listpos
     FOR XML PATH(''), TYPE).value(N'./text()[1]', N'nvarchar(max)')
)
FROM x GROUP BY id;

Note that, in addition to guaranteeing order and being backward compatible (well, only back so many versions), it also ignores garbage data, e.g. try:

0001, ABCD1234;0002 but no comma

Upvotes: 1

Related Questions