SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


The Joy of Numbers


The Joy of Numbers

Author
Message
Mike C
Mike C
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6133 Visits: 1172
_ms65g_ (5/28/2010)
Remove duplicate side-by-side characters from a string
New approach using numbers table

CREATE FUNCTION dbo.fnRemoveDupesI (@String VARCHAR(8000)) 
RETURNS VARCHAR(8000) 
AS
BEGIN
    DECLARE @result VARCHAR(8000) = '';
   
   ;WITH DataOrder
   AS
   (
      SELECT ID, Data
             ,ROW_NUMBER() OVER (PARTITION BY Data ORDER BY ID) AS RowNum
        FROM (SELECT SUBSTRING(@String, nbr, 1), nbr 
                FROM Nums 
               WHERE nbr <= LEN(@String)
             ) D(data, ID)
   )
  
  SELECT @result = @result + Data
    FROM (SELECT ID, Data
                 ,DENSE_RANK() OVER (ORDER BY ID - RowNum) As [Rank]
            FROM DataOrder
         )D
   GROUP BY Data, [Rank]
   ORDER BY MIN(ID)

   RETURN @result
END;



Be careful with the multirow concatenation thing you got going on there. You might be better off using the FOR XML PATH subquery method for ordered string concatenation (but then you have to worry about XML entities).

Thanks
Mike C
_ms65g_
_ms65g_
SSC Veteran
SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)

Group: General Forum Members
Points: 238 Visits: 880
Hi,
Here is a new approach for splitting a delimited string. It is set-based, without CHARINDEX, using number table…
See:

DECLARE @s VARCHAR(620)='item_01,item_02,item_03';
SET @s = ','+ @s + ',';

;WITH C AS
(SELECT n, ROW_NUMBER() OVER(ORDER BY n) AS rn
FROM Nums --Number Table
WHERE SUBSTRING(@s, n, 1) = ','
AND n <= LEN(@s))
SELECT SUBSTRING(@s, A.n + 1, B.n - 1 - A.n) AS item
FROM C A
INNER JOIN C B
ON A.rn + 1 = B.rn
AND B.n - A.n > 1;



Also here is the simplified version of my technique for removing duplicate side-by-side characters.

   DECLARE @result VARCHAR(8000) = '',
@string VARCHAR(8000) = 'SQQQQQQQQQQLLLLLLL Serrrrrrrrveerr';

SELECT @result = @result + Data
FROM (SELECT ID,
Data,
ROW_NUMBER() OVER (PARTITION BY Data ORDER BY ID) - ID
FROM (SELECT SUBSTRING(@String, n, 1), n
FROM Nums
WHERE n <= LEN(@String)
) D(data, ID)
) D(ID, Data, RowNum)
GROUP BY Data, RowNum
ORDER BY MIN(ID)

SELECT @result AS Result


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)

Group: General Forum Members
Points: 202938 Visits: 41947
_ms65g_ (8/17/2010)
Hi,
Here is a new approach for splitting a delimited string. It is set-based, without CHARINDEX, using number table…
See:

DECLARE @s VARCHAR(620)='item_01,item_02,item_03';
SET @s = ','+ @s + ',';

;WITH C AS
(SELECT n, ROW_NUMBER() OVER(ORDER BY n) AS rn
FROM Nums --Number Table
WHERE SUBSTRING(@s, n, 1) = ','
AND n <= LEN(@s))
SELECT SUBSTRING(@s, A.n + 1, B.n - 1 - A.n) AS item
FROM C A
INNER JOIN C B
ON A.rn + 1 = B.rn
AND B.n - A.n > 1;




That method is quite old, actually. Most people don't use it because, since it actually has to read the numbers table twice, it's usually slower than the CHARINDEX method.

If you'd like to convert the code (I don't want to be accused of writting biased code :-D) so that it can be played against a table with a CSV column in it, I'd be happy to demonstrate/backup my claim with code.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search