Change lower case to upper (title) case using sql query

风格不统一 提交于 2019-12-08 18:11:26

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 ;

Example:

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

Output:

This Is Exactly The Same!

Copyrights:

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

Hope this helps!

Here is another Microsoft SQL function:

 CREATE FUNCTION PROPERCASE(@TEXT AS VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
   DECLARE @RESET BIT;
   DECLARE @STR VARCHAR(MAX);
   DECLARE @I INT;
   DECLARE @C CHAR(1);

   SELECT @RESET = 1, @I=1, @STR = '';

   WHILE (@I <= LEN(@TEXT))
    SELECT @C= SUBSTRING(@TEXT,@I,1),
               @STR = @STR + CASE WHEN @RESET=1 THEN UPPER(@C) ELSE LOWER(@C) END,
               @RESET = CASE WHEN @C LIKE '[A-ZA-Z]' THEN 0 ELSE 1 END,
               @I = @I +1
   RETURN @STR
END

This SQL should work.

SELECT UPPER(LEFT(<ColumnName>, 1)) + LOWER(RIGHT(<ColumnName>,LEN(<ColumnName>)-1)) FROM {YourTableName}

First you need to create function


CREATE FUNCTION ProperCase(@OriginalText VARCHAR(8000))
RETURNS VARCHAR(8000)  
BEGIN 
DECLARE @CleanedText VARCHAR(8000) 
;with
  a1 as (select 1 as N union all select 1 union all
         select 1      union all select 1 union all
         select 1      union all select 1 union all
         select 1      union all select 1 union all
         select 1      union all select 1),
  a2 as (select 1 as N from a1 as a cross join a1 as b),
  a3 as (select 1 as N from a2 as a cross join a2 as b),
  a4 as (select 1 as N from a3 as a cross join a2 as b),
  Tally as (select top (len(@OriginalText)) row_number() over (order by N) as N from a4)

SELECT @CleanedText = ISNULL(@CleanedText,'') +  
     --first char is always capitalized?
CASE WHEN Tally.N = 1 THEN UPPER(SUBSTRING(@OriginalText,Tally.N,1))
     WHEN SUBSTRING(@OriginalText,Tally.N -1,1) = ' '  THEN UPPER(SUBSTRING(@OriginalText,Tally.N,1))
     ELSE LOWER(SUBSTRING(@OriginalText,Tally.N,1))
END

FROM Tally           WHERE Tally.N 

Now you just use this function

select dbo.ProperCase('My nAme is iShAn halaRNkar')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!