Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace first occurrence of substring in a string in SQL

Tags:

I have to fetch data from a @temp table which has something like "or ccc or bbb or aaa" I want to replace the first occurrence into space to get something like this " ccc or bbb or aaa". I am trying stuff and replace but they don't seem to get me the desired result

What I have tried:

DECLARE @stringhere as varchar(500)  DECLARE @stringtofind as varchar(500)  set @stringhere='OR contains or cccc or  '  set @stringtofind='or' select STUFF('OR contains or cccc or  ',PATINDEX('or', 'OR contains or cccc or  '),0 ,' ') 
like image 881
CrazySwazy Avatar asked Aug 12 '16 06:08

CrazySwazy


People also ask

How do I change the first occurrence of a string in SQL?

CHARINDEX('substring', col) will return the index of the first occurrence of 'substring' in the column. STUFF then replaces this occurrence with 'replacement' .

How do you replace a substring in a string in SQL?

SQL Server REPLACE() Function The REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive.

How do I find the first occurrence of a string in SQL Server?

Searching from the start of a string expression. This example returns the first location of the string is in string This is a string , starting from position 1 (the first character) of This is a string . SELECT CHARINDEX('is', 'This is a string');

How do I change the first 3 characters in SQL?

In a view, you could do it like: select case when col1 like '00%' then stuff(col1, 1, 2, '11') else col1 end from YourTable; Live example at SQL Fiddle. Just a note, the substring should be "substring(col1, 3, len(col1)-2)"because you want to start at 3rd character and the characters are numbered from 1, not 0.

How do you replace a substring in SQL?

Code language: SQL (Structured Query Language) (sql) In this syntax: input_string is any string expression to be searched. substring is the substring to be replaced. new_substring is the replacement string. The REPLACE() function returns a new string in which all occurrences of the substring are replaced by the new_substring.

How to replace all instances of a string with another string?

In SQL Server, you can use the T-SQL REPLACE () function to replace all instances of a given string with another string. For example, you can replace all occurrences of a certain word with another word.

How do you replace a string with another string in Python?

The REPLACE function will search for all occurrences of the old_substring and replace it with the new_string. The following statement replaces all the occurrences of bar with foo so the result is bar bar bar. Note that the REPLACE function searches for the substring in the case sensitive manner.

How to perform search/replace for only the first occurrence in MySQL?

Perform search/replace for only the first occurrence of a character in MySQL table records? You can achieve this with the help of CONCAT () along with REPLACE () function.


2 Answers

You can use a combination of STUFF and CHARINDEX to achieve what you want:

SELECT STUFF(col, CHARINDEX('substring', col), LEN('substring'), 'replacement') FROM #temp 

CHARINDEX('substring', col) will return the index of the first occurrence of 'substring' in the column. STUFF then replaces this occurrence with 'replacement'.

like image 104
Tim Biegeleisen Avatar answered Sep 22 '22 21:09

Tim Biegeleisen


it seems you miss 2% preceding and trailing to the target string

please try:

select STUFF(@stringhere, PATINDEX('%' + @stringtofind + '%', @stringhere), LEN(@stringtofind), ' ') 
like image 28
Grace Avatar answered Sep 22 '22 21:09

Grace