Remove a given substring 'n' number of times from the end of a string

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

DECLARE @ varchar(99)= 'Cats do meow-meow while lions do not meow.'
DECLARE @r varchar(max)= 'meow'
DECLARE @n INT = 2;

WITH c(z)as(SELECT 1UNION ALL
SELECT charindex(@r,@,z+1)FROM c WHERE z>0)SELECT top(@n)@=stuff(@,z,len(@r),'')FROM
c ORDER BY-z PRINT @

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results