Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I achieve initcap functionality in MySQL?

Tags:

mysql

I need the functionality of initcap for the region names. Is there any way to achieve this in MySQL?

  • Africa
  • India Sub Continent
  • U.S. West Coast
  • Europe / U.K

in this way the results can be?

like image 310
Rajesh Avatar asked Sep 11 '12 06:09

Rajesh


People also ask

What is Initcap in MySQL?

The INITCAP function converts the first letter of each word in a string to uppercase, and converts any remaining characters in each word to lowercase. Words are delimited by white space characters, or by characters that are not alphanumeric.

How do I use Initcap?

The INITCAP function in PLSQl is used for setting the first character in each word to uppercase and the rest to lowercase. Words are delimited by white space or characters that are not alphanumeric. The INITCAP function in PLSQL can accept char can of any of the datatypes such as CHAR, VARCHAR2, NCHAR, or NVARCHAR2.

How do I find the length of a string in MySQL?

MySQL LENGTH() Function The LENGTH() function returns the length of a string (in bytes).


4 Answers

Sometime back I was looking for a built-in initcap/ucfirst function in MySQL but unfortunately couldn't find such string functions so decided to write my own.. thanks to the MySQL community member who corrected the bug in my function & posted it back.

DELIMITER $$

DROP FUNCTION IF EXISTS `test`.`initcap`$$

CREATE FUNCTION `initcap`(x char(30)) RETURNS char(30) CHARSET utf8
READS SQL DATA
DETERMINISTIC
BEGIN
SET @str='';
SET @l_str='';
WHILE x REGEXP ' ' DO
SELECT SUBSTRING_INDEX(x, ' ', 1) INTO @l_str;
SELECT SUBSTRING(x, LOCATE(' ', x)+1) INTO x;
SELECT CONCAT(@str, ' ', CONCAT(UPPER(SUBSTRING(@l_str,1,1)),LOWER(SUBSTRING(@l_str,2)))) INTO @str;
END WHILE;
RETURN LTRIM(CONCAT(@str, ' ', CONCAT(UPPER(SUBSTRING(x,1,1)),LOWER(SUBSTRING(x,2)))));
END$$

DELIMITER ;


Usage: 

select initcap('This is test string');
like image 114
Nimit Dudani Avatar answered Oct 02 '22 19:10

Nimit Dudani


DELIMITER $$

CREATE
   FUNCTION `test`.`initcap`(input VARCHAR(255))
   RETURNS VARCHAR(255) 
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$$

DELIMITER ;

usage: initcap("hello world") output: Hello World

usage: initcap("HELLO WORLD") output: Hello World

This converts first letter of each word in the string to capital.

like image 44
Usman_Enterprise_Developer Avatar answered Oct 02 '22 19:10

Usman_Enterprise_Developer


DELIMITER $$

DROP FUNCTION IF EXISTS `test`.`initcap`$$

CREATE FUNCTION `initcap`(x char(30)) RETURNS char(30) CHARSET utf8
BEGIN
SET @str='';
SET @l_str='';
WHILE x REGEXP ' ' DO
SELECT SUBSTRING_INDEX(x, ' ', 1) INTO @l_str;
SELECT SUBSTRING(x, LOCATE(' ', x)+1) INTO x;
SELECT CONCAT(@str, ' ', CONCAT(UPPER(SUBSTRING(@l_str,1,1)),LOWER(SUBSTRING(@l_str,2)))) INTO @str;
END WHILE;
RETURN LTRIM(CONCAT(@str, ' ', CONCAT(UPPER(SUBSTRING(x,1,1)),LOWER(SUBSTRING(x,2)))));
END$$

DELIMITER ;
like image 32
Chiran Avatar answered Oct 02 '22 18:10

Chiran


CREATE FUNCTION `upperfirst`(x varchar(255)) RETURNS varchar(255) CHARSET latin1

RETURN concat( upper(substring(x,1,1)),lower(substring(x,2)) );
like image 26
my-c0p Avatar answered Oct 02 '22 18:10

my-c0p