使用SQL查询将小写更改为大写

使用SQL查询将小写更改为大写

本文介绍了使用SQL查询将小写更改为大写(标题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用sql查询更改大小写

i want to change case using sql query

例如,如果文本为:My nAme is iShAn halaRNkar(文本混乱,则可能在senetence中的任何地方包含小写或大写)

e.g if text is : My nAme is iShAn halaRNkar (text is jumbled i.e it may contain Lower case or Upper case anywhere in the senetence)

比我希望的输出是:My Name Is Ishan Halarnkar

我对SQL查询的工作还不多.请帮助.

i have not worked on sql queries much. Kindly help.

推荐答案

在任何数据库中都没有为您执行此操作的函数.您必须编写一个函数,该函数实际上对句子中的每个单词执行检查.请检查以下解决方案:

There's no such function in any database which do this for you. You've to write a function which actually performs the check on each word in a sentence. Please check the solutions below:

MySql:

DELIMITER //

    CREATE FUNCTION CAP_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
            IF (MID(input,i,1) = ' ' OR i = 0) THEN
                IF (i < len) THEN
                    SET input = CONCAT(
                        LEFT(input,i),
                        UPPER(MID(input,i + 1,1)),
                        RIGHT(input,len - i - 1)
                    );
                END IF;
            END IF;
            SET i = i + 1;
        END WHILE;

        RETURN input;
    END//

DELIMITER ;

示例:

SELECT CAP_FIRST('this is exACtly tHe same!')

输出:

This Is Exactly The Same!

版权:

http://joezack.com/2008/10/20/mysql -capitalize-function/

希望这会有所帮助!

这篇关于使用SQL查询将小写更改为大写(标题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:26