Wednesday 31 January 2024

Replace, Substring and Words Count in MySQL

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

Create .ICS file using PHP code

Recently worked on creating a .ics file in PHP after a very long time, code so thought to share with everybody. Please find below the comple...