Skip to content

Instantly share code, notes, and snippets.

@nachodd
Created September 13, 2013 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nachodd/6550973 to your computer and use it in GitHub Desktop.
Save nachodd/6550973 to your computer and use it in GitHub Desktop.
Dada una determianda cadena de caracteres, devuelve la misma en formato "Camel Case". Given a string, returns it in Camel Case. #USING: SELECT pCase( 'esto Es uNA prueba uk eSTa en miNusucla' ) FROM DUAL; #OUTPUT: Esto Es Una Prueba UK Esta En Minusucla
DELIMITER $$
DROP FUNCTION IF EXISTS `pCase` $$
CREATE DEFINER=`root`@`localhost` FUNCTION `pCase`(str TEXT) RETURNS text CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE result TEXT default '';
DECLARE word TEXT default '';
DECLARE working_char TEXT default '';
DECLARE last_space INT default 0;
DECLARE word_boundry TEXT default ' (){}[]-/'; #Used to decide when a new word begins. Add you boundries here!!
DECLARE ucase_words TEXT default 'UK,USA,DVD,DVDs,GB'; #To make some words uppercase use ucase_words below. The text entered here is used so dvds becomes DVDs.
DECLARE i INT default 1; #Loop counter
DECLARE found_boundry INT default 1; #When we find a boundry set to 1 (True) else 0 (False)
# handle NULL
IF (str IS NULL) THEN
RETURN NULL;
END IF;
# if 0 length string given
IF (char_length(str) = 0) THEN
RETURN '';
END IF;
SET str = lower(str);
# loop through each letter looking for a word boundry
WHILE(i <= (length(str)+1)) DO
#Set our working charater
SET working_char=SUBSTRING(str, i-1, 1);
#Find a word boundry
IF(locate(working_char, word_boundry)>0) THEN
#Check if last word was in our uppercase list, using the example in the list to allow dvds to become DVDs
IF(locate(word, ucase_words)>0) THEN
SET result=CONCAT(LEFT(result,(LENGTH(result)-LENGTH(word))),MID(ucase_words,locate(word, ucase_words),LENGTH(word)));
END IF;
SET found_boundry=1; #Set the boundry flag, then ignore
SET result=CONCAT(result, working_char);
SET word=''; #Reset word
ELSE
SET word=CONCAT(word, working_char);
IF(found_boundry=1) THEN
SET result = CONCAT(result, UPPER(working_char)); #After a boundry so upper case
SET found_boundry=0;
ELSE
SET result = CONCAT(result, working_char);
END IF;
END IF;
SET i=i+1;
END WHILE;
#Check if last word was in our uppercase list
IF(locate(word, ucase_words)>0) THEN
SET result=CONCAT(LEFT(result,(LENGTH(result)-LENGTH(word))),MID(ucase_words,locate(word, ucase_words),LENGTH(word)));
END IF;
RETURN result;
END $$
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment