user3224778
user3224778

Reputation: 1

Sql query bracket - replacing all text with a single character

I have a situation where i need to replace the entire text and the square bracket with a single charecter inside stored procedure. ( Iam using Sql Server 2012).

Eg:->

Let us consider that i have a text san[123456dd]text

i just wanted to replace all the text inside the square bracket and square bracket itself with another charecter say 'X'

Here, My end result should be sanXtext.

Could anyone help me regarding this?

Upvotes: 0

Views: 2447

Answers (2)

M.Ali
M.Ali

Reputation: 69564

DECLARE @String VARCHAR(1000) = 'san[123456dd]text'

SELECT LEFT(@String, CHARINDEX('[', @String)-1) +'X' +
       RIGHT(@String, CHARINDEX(']', REVERSE(@String))-1)

Result: sanXtext

Upvotes: 1

Mihai
Mihai

Reputation: 26804

SELECT REPLACE(col,SUBSTRING(col, (CHARINDEX('[',col)), CHARINDEX(']',col) - 
          (CHARINDEX('[', col)) + Len(']')),'x')
  FROM tableName

Fiddle

Upvotes: 0

Related Questions