1

I have the below text to replace in sql server. Is there any query to do so? The text is found in a column of a table.

Text before replace:

[demo].[demo_df__table_list__enum_table]

I want to replace the text and result should appear like this:

[[demo]].[demo_df__table_list__enum_table]]]
5
  • could you cite some more examples? Commented May 6, 2014 at 12:31
  • Where do you want to replace? an procedure? Try to be more clear Commented May 6, 2014 at 12:32
  • TExt where? In a data field or in code (like a SP)? Commented May 6, 2014 at 12:33
  • the value is stored in a variable and that variable should be replaced Commented May 6, 2014 at 12:33
  • [demo].[demo_df__table_list__enum_table] is stored in a variable. the query should check the format and replace the same Commented May 6, 2014 at 12:35

3 Answers 3

4

try this !

declare @a as varchar(100)
set @a ='[demo].[demo_df__table_list__enum_table]'
select QUOTENAME(@a)

see it live

Sign up to request clarification or add additional context in comments.

Comments

2

Yes, you can replace text in a column, for a row, like this:

UPDATE tbl
SET fld = REPLACE(fld,
    '[demo].[demo_df__table_list__enum_table]',
    '[[demo]].[demo_df__table_list__enum_table]]]')
WHERE [some condition here]

UPDATE: I see that you can use QUOTENAME as shown below, that's a good approach, but it still does you no good without the UPDATE statement. So the query would be:

UPDATE tbl
SET fld = REPLACE(fld, fld, QUOTENAME(fld))
WHERE [some condition here]

1 Comment

@user3425160, well now that the OP has updated their question I now know what text they are trying to replace with what. But the OP needs to leverage the REPLACE command to update a field in a table with new text. I'm not sure how it doesn't answer the question.
0

Replace is a function which takes three parametere: i;e Replace(Column Name,string literal to be replaced in Column,string literal to placed.) For examples G_DATE is Column 2015-03-09 2015-03-09 2015-03-09 2015-03-10 2015-03-10 2015-03-10 2015-03-12 2015-03-12 2015-03-12

UPDATE table SET G_DATE = REPLACE(G_DATE,'15','16') will give us

2016-03-09 2016-03-09 2016-03-09 2016-03-10 2016-03-10 2016-03-10 2016-03-12 2016-03-12 2016-03-12

Comments

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.