关于sql server:使用sql查询将小写改为upper(title)case

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

我想使用SQL查询更改案例

例如,如果文本为:My nAme is iShAn halaRNkar(文本混乱,即在衰老过程中可能包含小写或大写)

我不希望输出是:My nAme is iShAn halaRNkar

我在SQL查询方面的工作不多。请帮忙。


下面是另一个Microsoft SQL函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 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


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

MySql:

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
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 ;

例子:

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

输出:

1
This IS Exactly The Same!

版权:

MySQL Capitalize Function

希望这有帮助!


首先需要创建函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

现在您只需使用这个函数

1
SELECT dbo.ProperCase('My nAme is iShAn halaRNkar')


这个SQL应该可以工作。

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