我想使用sql查询更改案例
例如,如果文本是:My nAme is iShAn halaRNkar
(文本混淆,即在可能的任何地方可能包含小写或大写)
比我希望输出为:My Name Is Ishan Halarnkar
我没有在sql查询上工作过多。请帮助。
答案 0 :(得分:6)
这是另一个Microsoft SQL函数:
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
答案 1 :(得分:3)
在任何数据库中都没有为您执行此操作的功能。你要写一个实际上对句子中每个单词进行检查的函数。请检查以下解决方案:
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/
希望这有帮助!
答案 2 :(得分:1)
这个SQL应该可以工作。
SELECT UPPER(LEFT(<ColumnName>, 1)) + LOWER(RIGHT(<ColumnName>,LEN(<ColumnName>)-1)) FROM {YourTableName}
答案 3 :(得分:1)
首先你需要创建功能
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')