1

In T-SQL, how do I replace the occurrence of a substring only at the end of a character string (varchar)?

For example:

If I were to do the operation with substring violence on character string 'violence begets violence', the result would be 'violence begets ' Some other examples are

1)'the fox jumped over the fence' with substring fox would result in no change as fox is not at the end of the character string.

2)Can I kick the can with substring can would result in Can I kick the

3)gs_fs_pringles with substring _fs would result in no changes

4)gs_pringles_fs with substring _fs would result in gs_pringles

2
  • 1
    Why does gs_pringles_fs become gs_pringles? gs is not fs? Commented May 30, 2013 at 15:37
  • 1
    Cuz the replace string is _fs Commented May 30, 2013 at 16:59

2 Answers 2

3
DECLARE @t TABLE(SomeString varchar(100))

INSERT @t VALUES ('violence begets violence'), ('the fox jumped over the fence'), ('Can I kick the can');

WITH CTE1 AS (
    SELECT
        CHARINDEX(' ', SomeString) AS LeftIndex,
        REVERSE(CHARINDEX(' ', SomeString)) AS RightIndex,
        RTRIM(SomeString) AS SomeString
    FROM
        @t
), CTE2 AS (
    SELECT
       LEFT(SomeString, LeftIndex-1) AS LeftWord,
       RIGHT(SomeString, RightIndex-1) AS RightWord,
       LeftIndex, RightIndex, SomeString
    FROM  
       CTE1
)
SELECT 
    CASE
        WHEN LeftWord <> RightWord THEN SomeString
        ELSE LEFT(SomeString, LEN(SomeString)-RightIndex)
    END
FROM
    CTE2;
Sign up to request clarification or add additional context in comments.

8 Comments

Sorry. It came pretty close but doesnt work for 'the fox jumped over the fence' with substring fox. It truncates the original character string
:(. No single line answer? I wanted to be able to use it in another query.
@JayJayJay: you can, but it becomes very messy. And it gets messier as you add more samples...
Where are you specifying the replacement string/substring?
And you can take the string processing into a UDF. You are allowed to think for yourself.
|
0

The following ignores trailing spaces. If that is not a problem, please give it a try:

SELECT
  acolumn = CASE
    WHEN LEN(acolumn) < LEN(@substring)
    THEN acolumn
    ELSE LEFT(acolumn, x.cutpos)
       + REPLACE(SUBSTRING(acolumn, x.cutpos + 1, 999999), @substring, '')
  END
FROM atable
CROSS APPLY (SELECT LEN(acolumn) - LEN(@substring)) x (cutpos)
;

The query works like this:

  1. If the length of the column value is less than the length of the argument substring, the entire column value is returned.

  2. Otherwise, the original value is divided into two pieces: the same number of characters as in the argument is cut out from the right, the rest of the value forms the left part.

  3. If the right part "contains" (but in fact is equal to) the argument, it is replaced with an empty string, otherwise it remains unchanged.

  4. In either event, the result is concatenated back to the left part of the original value to form the final output value.

There's also a SQL Fiddle for you to play with.

3 Comments

Pretty nice the sql fiddle
cutpos is a column produced by the CROSS APPLY clause. It is the difference between the length of the column value and the length of the argument. If you really need a single expression, you can remove the CROSS APPLY clause and replace each x.cutpos entry with LEN(acolumn) - LEN(@substring). That would give you the same result.
I'll approve and upvote the answer once I get a chance to run. I am just a little tied up right now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.