how to replace special characters from mysql string - mysql

there is a table in mysql database and it has strings, datatype is text, and the data which is inserted is as below
HELLO+THIS+IS+OFFER!!%+HURRY!!%0ACall+US+NOW%
How do I remove % and + special characters from this string, it looks like encoded string.
Best Regards
CJ

Below mysql function removes special characters from a string:
DROP FUNCTION IF EXISTS replacespecialchars;
DELIMITER |
CREATE FUNCTION replacespecialchars( str CHAR(255) ) RETURNS CHAR(255) DETERMINISTIC
BEGIN
DECLARE i, len SMALLINT DEFAULT 1;
DECLARE ret CHAR(255) DEFAULT '';
DECLARE c CHAR(1);
SET len = CHAR_LENGTH( str );
REPEAT
BEGIN
SET c = MID( str, i, 1 );
IF c REGEXP '[[:alnum:]]' THEN
SET ret=CONCAT(ret,c);
END IF;
SET i = i + 1;
END;
UNTIL i > len END REPEAT;
RETURN ret;
END |
DELIMITER ;

Related

How to convert text to proper case using mysql command line

What is the best method to convert text to proper case using MYSQL command line
Example: JOHN DOE convert to John Doe
as answered in a previous post here Capitalize first letter. MySQL if you create the following function in the database, then it can be used on the command line also, this will capitalise every word on the string.
DELIMITER ||
CREATE FUNCTION `UC_Words`( str VARCHAR(255) ) RETURNS VARCHAR(255) CHARSET utf8_general_ci
BEGIN
DECLARE c CHAR(1);
DECLARE s VARCHAR(255);
DECLARE i INT DEFAULT 1;
DECLARE bool INT DEFAULT 1;
DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!#;:?/';
SET s = LCASE( str );
WHILE i < LENGTH( str ) DO
BEGIN
SET c = SUBSTRING( s, i, 1 );
IF LOCATE( c, punct ) > 0 THEN
SET bool = 1;
ELSEIF bool=1 THEN
BEGIN
IF c >= 'a' AND c <= 'z' THEN
BEGIN
SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
SET bool = 0;
END;
ELSEIF c >= '0' AND c <= '9' THEN
SET bool = 0;
END IF;
END;
END IF;
SET i = i+1;
END;
END WHILE;
RETURN s;
END ||
DELIMITER ;

MySQL - Capital first letter of each word

Below is MySQL function to capitalize the first letter of every word in a string.
USE `db`$$
DROP FUNCTION IF EXISTS `UC_Words`$$
CREATE DEFINER=`root`#`localhost` FUNCTION `UC_Words`(str VARCHAR(255) ) RETURNS VARCHAR(255) CHARSET latin1
MODIFIES SQL DATA
DETERMINISTIC
BEGIN
DECLARE c CHAR(1);
DECLARE s VARCHAR(255);
DECLARE i INT DEFAULT 1;
DECLARE BOOL INT DEFAULT 1;
DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!#;:?/';
SET s = LCASE( str );
WHILE i < LENGTH( str ) DO
BEGIN
SET c = SUBSTRING( s, i, 1 );
IF LOCATE( c, punct ) > 0 THEN
SET BOOL = 1;
ELSEIF BOOL=1 THEN
BEGIN
IF c >= 'a' AND c <= 'z' THEN
BEGIN
SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
SET BOOL = 0;
END;
ELSEIF c >= '0' AND c <= '9' THEN
SET BOOL = 0;
END IF;
END;
END IF;
SET i = i+1;
END;
END WHILE;
RETURN s;
END$$
DELIMITER ;
It's works fine for all Strings. But if I provide string "M R E" then its give "M R e" output.
plz suggest.
First execute below query
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `UC_FIRST`(oldWord VARCHAR(255)) RETURNS varchar(255) CHARSET latin1
RETURN CONCAT(UCASE(SUBSTRING(oldWord, 1, 1)),SUBSTRING(oldWord, 2))
Then execute below query
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `UC_Words`(oldName VARCHAR(255), delim VARCHAR(1), trimSpaces BOOL) RETURNS varchar(255) CHARSET latin1
BEGIN
SET #oldString := oldName;
SET #newString := "";
tokenLoop: LOOP
IF trimSpaces THEN SET #oldString := TRIM(BOTH " " FROM #oldString); END IF;
SET #splitPoint := LOCATE(delim, #oldString);
IF #splitPoint = 0 THEN
SET #newString := CONCAT(#newString, UC_FIRST(#oldString));
LEAVE tokenLoop;
END IF;
SET #newString := CONCAT(#newString, UC_FIRST(SUBSTRING(#oldString, 1, #splitPoint)));
SET #oldString := SUBSTRING(#oldString, #splitPoint+1);
END LOOP tokenLoop;
RETURN #newString;
END
Then after call function
SELECT UC_Words("this is for testing"," ", TRUE);
Output below
This Is For Testing

remove all numeric characters from column mysql

I want to develop one mysql function that can remove only numeric characters from the string.
You can write a user defined function, where in you can write your logic of replacement or you can try :
Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(column,'9',''),'8',''),'7',''),'6',''),'5',''),'4',''),'3',''),'2',''),'1',''),'0','')
Create function to achieve this task.
DROP FUNCTION IF EXISTS alphas;
DELIMITER |
CREATE FUNCTION alphas( str CHAR(32) ) RETURNS CHAR(16)
BEGIN
DECLARE i, len SMALLINT DEFAULT 1;
DECLARE ret CHAR(32) DEFAULT '';
DECLARE c CHAR(1);
SET len = CHAR_LENGTH( str );
REPEAT
BEGIN
SET c = MID( str, i, 1 );
IF c REGEXP '[[:alpha:]]' THEN
SET ret=CONCAT(ret,c);
END IF;
SET i = i + 1;
END;
UNTIL i > len END REPEAT;
RETURN ret;
END |
DELIMITER ;
SELECT alphas('123ab45cde6789fg');
+----------------------------+
| alphas('123ab45cde6789fg') |
+----------------------------+
| abcdefg |
+----------------------------+
If you want only digits, use this
SET GLOBAL log_bin_trust_function_creators=1;
DROP FUNCTION IF EXISTS digits;
DELIMITER |
CREATE FUNCTION digits( str CHAR(32) ) RETURNS CHAR(32)
BEGIN
DECLARE i, len SMALLINT DEFAULT 1;
DECLARE ret CHAR(32) DEFAULT '';
DECLARE c CHAR(1);
SET len = CHAR_LENGTH( str );
REPEAT
BEGIN
SET c = MID( str, i, 1 );
IF c BETWEEN '0' AND '9' THEN
SET ret=CONCAT(ret,c);
END IF;
SET i = i + 1;
END;
UNTIL i > len END REPEAT;
RETURN ret;
END |
DELIMITER ;
SELECT digits('123ab45cde6789fg');
+----------------------------+
| digits('123ab45cde6789fg') |
+----------------------------+
| 123456789 |
+----------------------------+
Reference

mysql question - way to update case for field?

I have some entries in a field that look like this: NEvada city or nevada City... Is there a way to update the database so that entries have initial caps: Nevada City?
You can use:
UPDATE `table` SET
`field` = CONCAT(UPPER(LEFT(`field`, 1)), LOWER(SUBSTRING(`field`, 2)))
But still you will need to modify it to allow a capital Letter for the next word like City...
Source here
UPDATE:
Found this example at MySQL's forum, it is exactly what you need:
DROP FUNCTION IF EXISTS proper;
SET GLOBAL log_bin_trust_function_creators=TRUE;
DELIMITER |
CREATE FUNCTION proper( str VARCHAR(128) )
RETURNS VARCHAR(128)
BEGIN
DECLARE c CHAR(1);
DECLARE s VARCHAR(128);
DECLARE i INT DEFAULT 1;
DECLARE bool INT DEFAULT 1;
DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!#;:?/';
SET s = LCASE( str );
WHILE i < LENGTH( str ) DO
BEGIN
SET c = SUBSTRING( s, i, 1 );
IF LOCATE( c, punct ) > 0 THEN
SET bool = 1;
ELSEIF bool=1 THEN
BEGIN
IF c >= 'a' AND c <= 'z' THEN
BEGIN
SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
SET bool = 0;
END;
ELSEIF c >= '0' AND c <= '9' THEN
SET bool = 0;
END IF;
END;
END IF;
SET i = i+1;
END;
END WHILE;
RETURN s;
END;
|
DELIMITER ;

MySQL: failed to create function {functionName}

This works on MySQL 5.0.41, but on 5.1.31 it just says "failed to create function".
I type this in the console:
delimiter |
<press enter>
CREATE DEFINER=`root`#`localhost` FUNCTION `ucwords`( str VARCHAR(128) ) RETURNS varchar(128) CHARSET utf8
BEGIN
DECLARE c CHAR(1);
DECLARE s VARCHAR(128);
DECLARE i INT DEFAULT 1;
DECLARE bool INT DEFAULT 1;
DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!#;:?/';
SET s = LCASE( str );
WHILE i < LENGTH( str ) DO
BEGIN
SET c = SUBSTRING( s, i, 1 );
IF LOCATE( c, punct ) > 0 THEN
SET bool = 1;
ELSEIF bool=1 THEN
BEGIN
IF c >= 'a' AND c <= 'z' THEN
BEGIN
SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
SET bool = 0;
END;
ELSEIF c >= '0' AND c <= '9' THEN
SET bool = 0;
END IF;
END;
END IF;
SET i = i+1;
END;
END WHILE;
RETURN s;
END |
<press enter>
I have even tried minimizing it to just:
delimiter |
<press enter>
CREATE DEFINER=`root`#`localhost` FUNCTION `ucwords`( str VARCHAR(128) ) RETURNS varchar(128) CHARSET utf8
BEGIN
DECLARE s VARCHAR(128);
RETURN s;
END |
<press enter>
I have even tried it without the definer, just using:
delimiter |
<press enter>
CREATE FUNCTION `ucwords`( str VARCHAR(128) ) RETURNS varchar(128) CHARSET utf8
BEGIN
DECLARE s VARCHAR(128);
RETURN s;
END |
<press enter>
Did you recently upgrade from MySQL 5.0 to 5.1? If so, you need to run the mysql_upgrade script to update the system tables. In this case, the mysql.proc table schema changed, which could explain your problem:
http://dev.mysql.com/doc/refman/5.1/en/mysql-upgrade.html
More on upgrading to 5.1:
http://dev.mysql.com/doc/refman/5.1/en/upgrading-from-previous-series.html