-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupperCase.sql
More file actions
38 lines (31 loc) · 1002 Bytes
/
upperCase.sql
File metadata and controls
38 lines (31 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Write a function to capitalize the first letter of a word in a given string.
*/
DROP FUNCTION IF EXISTS CAPITALIZE_FIRST;
CREATE FUNCTION CAPITALIZE_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
/*check if this is a space it's suppose that next symbol must be uppercase*/
IF (MID(input, i, 1) = ' ' OR i = 0)
THEN
IF (i < len)
THEN
SET input = CONCAT(
LEFT(input, i), /*Sliced everything that was left*/
UPPER(MID(input, i + 1, 1)), /*do the uppercase of next symbol after space*/
RIGHT(input, len - i - 1) /*Slice the rest of the String*/
); /*And CONCAT glue these pieces together*/
END IF;
END IF;
SET i = i + 1;
END WHILE;
RETURN input;
END;
SELECT CAPITALIZE_FIRST(" unITeD sTates of aMeriCA");