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 ;
Related
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 ;
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
I have a column in a mysql table that users have added data. Some users like to use proper capitalization, some like uppercase words, some all lower case.
What I want to do is change:
CHECK BUILDING.
To:
Check building.
And:
DELIVER PAPERS. UNLOCK DOORS.
To:
Deliver papers. Unlock doors.
I assume a function could do this so I can re-use it.
Any ideas?
I tried this but it does each word not the first word in a sentence.
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
There's no MySQL function to do that. This function capitalize the first letter of every word in a string.
CREATE FUNCTION CAP_FIRST (input VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE len INT;
DECLARE i INT;
SET len = CHAR_LENGTH(input);
SET input = LOWER(input);
SET i = 0;
WHILE (i < len) DO
IF (MID(input,i,1) = ' ' OR i = 0) THEN
IF (i < len) THEN
SET input = CONCAT(
LEFT(input,i),
UPPER(MID(input,i + 1,1)),
RIGHT(input,len - i - 1)
);
END IF;
END IF;
SET i = i + 1;
END WHILE;
RETURN input;
END;
References link.
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 ;
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