0

How can I REPLACE a string value with a other field content?

SELECT REPLACE(html_input,'!value',field_value) FROM table

Value of html_input is <input type='text' value='!value'>

Value of field_value is test

Expected result:

<input type='text' value='test'>
asked Nov 8, 2018 at 11:54
8
  • Please tag you MySQL version. Commented Nov 8, 2018 at 11:56
  • For any version Commented Nov 8, 2018 at 11:57
  • It does not matter is any parameter a literal or a field name. Commented Nov 8, 2018 at 12:15
  • Using a field does not work, returns a null field. Commented Nov 8, 2018 at 12:19
  • 1
    Do not use IF() - there is COALESCE(field_value, ''). Commented Nov 8, 2018 at 12:25

1 Answer 1

1

String literals must be enclosed by quotes '.

Quote symbols within a literal must be quoted by slash as \'.

SELECT REPLACE(html_input,'!value',field_value) 
FROM table
/* WHERE html_input = '<input type=\'text\' value=\'!value\'>'
 AND field_value = 'test';
*/
answered Nov 8, 2018 at 12:05
3
  • I had chosen words that confused the question and it was edited in the wrong way causing confusion. I reworded the question! Thank you Commented Nov 8, 2018 at 12:08
  • @user2175345 Your edition makes WHERE clause unnesessary. I'll comment it... Done. Commented Nov 8, 2018 at 12:13
  • This is not a query with WHERE. Is a replace in a string by a value of a field. See again Commented Nov 8, 2018 at 12:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.