Last active
March 12, 2025 19:35
-
-
Save jamalnasir/2cb9387deac373957623bfe331dc4f0f to your computer and use it in GitHub Desktop.
Get Camel Case of a String in MySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELIMITER $$ | |
CREATE FUNCTION `camel_case`(str varchar(128)) RETURNS varchar(128) | |
BEGIN | |
DECLARE n, pos INT DEFAULT 1; | |
DECLARE sub, proper VARCHAR(128) DEFAULT ''; | |
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; | |
set proper = concat_ws(' ', proper, concat(upper(left(sub,1)),substr(sub,2))); | |
set n = pos + 1; | |
END WHILE; | |
end if; | |
RETURN trim(proper); | |
END $$ | |
DELIMITER; | |
select camel_case('IRFAN ANSARI khan') as col; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
return concat(lower(left(proper,1)),substr(proper,2)); instead of proper will do the trick.