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'>
  • Please tag you MySQL version. – McNets Nov 8 '18 at 11:56
  • For any version – Spiderpoison Nov 8 '18 at 11:57
  • It does not matter is any parameter a literal or a field name. – Akina Nov 8 '18 at 12:15
  • Using a field does not work, returns a null field. – Spiderpoison Nov 8 '18 at 12:19
  • 1
    Do not use IF() - there is COALESCE(field_value, ''). – Akina Nov 8 '18 at 12:25
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';
*/
  • I had chosen words that confused the question and it was edited in the wrong way causing confusion. I reworded the question! Thank you – Spiderpoison Nov 8 '18 at 12:08
  • @user2175345 Your edition makes WHERE clause unnesessary. I'll comment it... Done. – Akina Nov 8 '18 at 12:13
  • This is not a query with WHERE. Is a replace in a string by a value of a field. See again – Spiderpoison Nov 8 '18 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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