Some useful string functions in MySQL.
If you want to replace a string in a given string.
SELECT name, REPLACE(name, 'Demo', 'Test') AS `replaced_string` FROM `table_names`;
If you want to get the first word in a given string.
SELECT name, SUBSTRING_INDEX(name, ' ', 1) AS first_word FROM `table_names`;
If you want to get the count of words in a string.
SELECT name, (LENGTH(name) - LENGTH(REPLACE(name, ' ', '')) + 1) AS word_count FROM `table_names`;
No comments:
Post a Comment