Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 remove side-by-side character at a string

Author  Topic 

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-18 : 06:42:23
[code]string (Before)
--------------------------------------------------
SSSSQQQLLL SSSSSeeeerrrrrveeer[/code]



string (After)
---------------------------
SQL Server

______________________

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-18 : 07:26:49
I am sure you can adapt madhi's solution:
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/06/squeeze-function.aspx


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-18 : 11:32:00
See
quote:
The Squeeze function is used to remove the multiple occurences of spaces into one occurence.


No.
My scenario is so different; the link technique is just for removing multiple spaces not other characters.


______________________
Go to Top of Page

mandm
Posting Yak Master

120 Posts

Posted - 2010-05-18 : 13:29:29
Whatever the solution, you'll need to consider what will happen to words where this occurs naturally. What will happen to words like Cheese, Squeeze, Pass, Pattern?
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-18 : 15:49:05
quote:
Originally posted by mandm

Whatever the solution, you'll need to consider what will happen to words where this occurs naturally. What will happen to words like Cheese, Squeeze, Pass, Pattern?


Cheese must be --> Chese
Squeeze --> Squeze

Remove duplicate side-by-side character from a string

______________________
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-19 : 01:17:03
quote:
Originally posted by ms65g

See
quote:
The Squeeze function is used to remove the multiple occurences of spaces into one occurence.


No.
My scenario is so different; the link technique is just for removing multiple spaces not other characters.


A space is just a character. That's why I thought you are able to adapt it.
But sure you can keep on waiting for a complete solution


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-19 : 01:35:56
Hi webfred,
Thanks . I have created a solution.

--Declaration string variables
DECLARE @s VARCHAR(500) = 'SSSSSSSSQQQQQQQLLLLLLLL Servvvvvvvvvverrrrr'
DECLARE @result VARCHAR(500)='';

--Publishing auxiliary sequence numbers table
WITH c AS
(SELECT 1 AS n
UNION ALL
SELECT 1 + n FROM c WHERE n < 100),
c1 AS (SELECT n = ROW_NUMBER() OVER (ORDER BY (SELECT 1))
FROM c c1
CROSS JOIN c c2)

--Splitting the string with numbs table
, k AS
(SELECT n, k = SUBSTRING (@s, n, 1)
FROM c1
WHERE n <= LEN(@s))

--Filtering characters and then concatenating them
SELECT @result = @result + k
FROM k k1
WHERE NOT EXISTS
(SELECT *
FROM k k2
WHERE k1.k = k2.k
AND k1.n+1 = k2.n);


SELECT @result AS removed;


______________________
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-19 : 01:45:29
quote:
Originally posted by webfred

quote:
Originally posted by ms65g

See
quote:
The Squeeze function is used to remove the multiple occurences of spaces into one occurence.


No.
My scenario is so different; the link technique is just for removing multiple spaces not other characters.


A space is just a character. That's why I thought you are able to adapt it.
But sure you can keep on waiting for a complete solution


No, you're never too old to Yak'n'Roll if you're too young to die.



What if we have 26 different characters in the string? Do not we need to write 26 separated statement for replace (or nested replace function) function?

______________________
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-19 : 01:51:38
a simple while loop to read each character and compare with previous char will do the job.

If your data is in a table, just create a UDF with above logic to do it.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-19 : 01:53:09
Using numbers table is not a better solution against WHILW loop?



______________________
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-19 : 02:04:00
quote:
Originally posted by ms65g

Using numbers table is not a better solution against WHILW loop?

______________________




I am not saying that while loop is better. It is just simpler. Whatever method you choose, you will still need to traverse through every character the string to determine which character to retain which to remove. The required logic can be implemented easily within the while loop.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-19 : 02:31:04
I back with a method that uses Madhivanan logic.
But there is a problem. Result of SELECT CHARINDEX ('x', 'X') is 1 but I want to see 0 instead of 1, because x <> X. Could you point me?


DECLARE @s VARCHAR(MAX);
SET @s = 'SSSSSSSQQQQQQLLLLL SSSSSeeeervvvvveerr'

DECLARE @i INTEGER;
SET @i = 1;

WHILE @i <= 256
BEGIN
IF CHARINDEX(CHAR(@i), @s) > 0
SET @s = REPLACE(REPLACE(REPLACE(@s, CHAR(@i),CHAR(@i) + '~!@#$%^&*'), '~!@#$%^&*' + CHAR(@i),''), '~!@#$%^&*', '');
SET @i = @i + 1;
END

SELECT @s AS [After]
/*
After
-----------
sql server ----> I want thes be SQL Serve not sql server!
*/


______________________
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-19 : 02:46:08
quote:
Originally posted by khtan

a simple while loop to read each character and compare with previous char will do the job.

If your data is in a table, just create a UDF with above logic to do it.


KH
[spoiler]Time is always against us[/spoiler]




I implemented your suggestion logic, but following CODE does not remove duplicate spaces in the string. Why?



DECLARE @s VARCHAR(MAX);
SET @s = 'SSSSSSSQQQQQQLLLLL SSSSSeeeervvvvveerr'

DECLARE @r VARCHAR(MAX);
SET @r = '';

DECLARE @i INTEGER;
SET @i = 1;

WHILE @i <= LEN(@s)
BEGIN
IF @i = 1
SET @r = SUBSTRING(@s, 1, 1);
ELSE IF SUBSTRING(@s, @i, 1) <> SUBSTRING(@r, LEN(@r), 1)
SET @r = @r + SUBSTRING(@s, @i, 1);

SET @i = @i + 1;
END

SELECT @r AS [Result]

--See! But I need remove duplicate space characters during the function!
/*
Result
-------------------------
SQL Server
*/


______________________
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-19 : 04:35:16
[code]
WHILE @i <= LEN(@s)
BEGIN
SELECT @r = @r + SUBSTRING(@s, @i, 1)
WHERE SUBSTRING(@s, @i, 1) <> SUBSTRING(@s, @i - 1, 1)

SELECT @i = @i + 1
END
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-05-19 : 04:51:44
Thanks,

At last I find the mistake!
It is the new version
WHILE @i <= LEN(@s)
BEGIN
IF @i = 1
SET @r = SUBSTRING(@s, 1, 1);
ELSE IF SUBSTRING(@s , @i, 1) <> SUBSTRING(@r, LEN(@r+'1')-1, 1)
SET @r = @r + SUBSTRING(@s, @i, 1);

SET @i = @i + 1;
END


______________________
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-19 : 06:43:35
Did you try the performance of number table method vs the WHILE LOOP ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-19 : 09:37:26
This does exactly what you need
http://beyondrelational.com/blogs/madhivanan/archive/2007/12/29/remove-duplicate-characters-from-a-string.aspx

But you need to slightly change it

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -