How to do a 'Proper case' formatting of a mysql column?

前端 未结 5 1290
无人共我
无人共我 2020-12-09 15:32

Is it possible in mysql to format a column in Proper Case?

Example: Proper(\"ABSALOM\") = \"Absalom\"

I have searched a lot and I think MySQL d

5条回答
  •  青春惊慌失措
    2020-12-09 15:58

    -- set upper case the first char of every word in a string
    DROP FUNCTION IF EXISTS PROPER;
    DELIMITER $$
    CREATE FUNCTION PROPER(inputStr varchar(1500)) 
    RETURNS VARCHAR(1500)
    DETERMINISTIC
    BEGIN
        DECLARE x, y, result VARCHAR(1500);
        SET result = '';
        SET x = '';
        SET y = LOWER(TRIM(inputStr));
        WHILE CHAR_LENGTH(y) > 0 DO
            -- get next word
            SET x = SUBSTRING_INDEX(y, ' ', 1);
            -- set upper case the first char
            SET x = CONCAT(UPPER(SUBSTRING(x, 1, 1)), SUBSTRING(x, 2));
            -- the final s (greek language)               
            IF RIGHT(x,1) = 'σ' THEN
                SET x = CONCAT(left(x, CHAR_LENGTH(x) - 1),'ς');
            END IF;
            -- add word to result
            SET result = CONCAT(result, ' ', x);
            -- prepare y for next loop
            SET y = TRIM(SUBSTRING(y, CHAR_LENGTH(x) + 1));
        END WHILE;
        RETURN (TRIM(result));
    END$$
    DELIMITER;
    

提交回复
热议问题