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'>
8
  • Please tag you MySQL version.
    – McNets
    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.
    – Akina
    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, '').
    – Akina
    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';
*/
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.
    – Akina
    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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.