Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

SQL Replace Function

$
0
0

Hello Everyone!

I'm in a fix, I've come across a scenario where simple replace function isn't supporting me in writing good queries. As a result, I've to use Loop or Cursor. Here is the case;

I've a tables CustomComments with 2 columns:

ID (int), and Comment (nvarchar(max))

ID value = 12
Comment value = $$$$ !!! @@@ ????? ? ? ? gear run successs&

See, there are repeated characters in comments, I've used CLR RegEx function to find out the repeated characters and those are:

Id	OldValue	NewValue
12	$$$$		$
12	!!!		!
12	@@@		@
12	????? ? ? ?	?

I want to replace each old value (i.e. repated value) in Table with NewValue

Here is my query to solve this purpose (initially i thought so)

UPDATE	c
	SET c.Comments = REPLACE(c.Comments, r.OldValue, r.NewValue)
FROM	CustomerComments c
		INNER JOIN 
		(
		SELECT  Id, t.Value OldValue, LEFT(t.Value, 1) NewValue
		FROM	cmts_t1 c (NOLOCK)
				CROSS APPLY dbo.RegexMatches(c.comments_normalized, '([^0-9\w])\1{1,}(\s*\1+)*') t
		) r ON c.Id = r.Id

What it does - it simply updates 1st record and nothing else.

I'm looking for a solution, where I can update my results with more results in subquery without using Loop or Cursor. 

Please help me in finding out solutions.

Remain Blessed

-Fazal


Remained Blessed -Naasfama



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>