Skip to content

Instantly share code, notes, and snippets.

@jamalnasir
Last active July 14, 2025 21:00
Show Gist options
  • Save jamalnasir/2cb9387deac373957623bfe331dc4f0f to your computer and use it in GitHub Desktop.
Save jamalnasir/2cb9387deac373957623bfe331dc4f0f to your computer and use it in GitHub Desktop.
Get Camel Case of a String in MySQL
DELIMITER $$
CREATE FUNCTION `camel_case`(str VARCHAR(128)) RETURNS VARCHAR(128)
BEGIN
DECLARE n, pos INT DEFAULT 1;
DECLARE sub, camel VARCHAR(128) DEFAULT '';
DECLARE isFirstWord BOOLEAN DEFAULT TRUE;
IF LENGTH(TRIM(str)) > 0 THEN
WHILE pos > 0 DO
SET pos = LOCATE(' ', TRIM(str), n);
IF pos = 0 THEN
SET sub = LOWER(TRIM(SUBSTR(TRIM(str), n)));
ELSE
SET sub = LOWER(TRIM(SUBSTR(TRIM(str), n, pos - n)));
END IF;
IF isFirstWord THEN
SET camel = CONCAT(camel, sub);
SET isFirstWord = FALSE;
ELSE
SET camel = CONCAT(camel, UPPER(LEFT(sub, 1)), SUBSTR(sub, 2));
END IF;
SET n = pos + 1;
END WHILE;
END IF;
RETURN camel;
END $$
DELIMITER ;
@cesarmarinhorj
Copy link

thanks!!!!

@daverogers
Copy link

doesn't work, looks like you're confused between camel_case and proper_case.

@t898374
Copy link

t898374 commented Mar 12, 2025

return concat(lower(left(proper,1)),substr(proper,2)); instead of proper will do the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment