| 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 |
|
|
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.______________________ |
 |
|
|
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? |
 |
|
|
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 --> CheseSqueeze --> SquezeRemove duplicate side-by-side character from a string______________________ |
 |
|
|
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. |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-05-19 : 01:35:56
|
Hi webfred,Thanks . I have created a solution.--Declaration string variablesDECLARE @s VARCHAR(500) = 'SSSSSSSSQQQQQQQLLLLLLLL Servvvvvvvvvverrrrr'DECLARE @result VARCHAR(500)=''; --Publishing auxiliary sequence numbers tableWITH 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 themSELECT @result = @result + kFROM k k1WHERE NOT EXISTS (SELECT * FROM k k2 WHERE k1.k = k2.k AND k1.n+1 = k2.n); SELECT @result AS removed; ______________________ |
 |
|
|
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?______________________ |
 |
|
|
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] |
 |
|
|
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?______________________ |
 |
|
|
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] |
 |
|
|
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 <= 256BEGIN IF CHARINDEX(CHAR(@i), @s) > 0 SET @s = REPLACE(REPLACE(REPLACE(@s, CHAR(@i),CHAR(@i) + '~!@#$%^&*'), '~!@#$%^&*' + CHAR(@i),''), '~!@#$%^&*', ''); SET @i = @i + 1;ENDSELECT @s AS [After]/*After-----------sql server ----> I want thes be SQL Serve not sql server!*/ ______________________ |
 |
|
|
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;ENDSELECT @r AS [Result]--See! But I need remove duplicate space characters during the function!/*Result-------------------------SQL Server*/ ______________________ |
 |
|
|
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 + 1END[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
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 versionWHILE @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 ______________________ |
 |
|
|
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] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|