19

我的数据库上有一个表。我的表的名称是“公司”。我想将数据“company_name”更改为大写首字母。例如;

“ABC公司”

“DEF 可塑性”

作为

“ABC公司”

“定义可塑性”

我知道我应该使用“UPDATE”命令。但是如何?谢谢你的帮助!

(CONCAT 不起作用)

4

7 回答 7

29

SQL Server 没有Initcaporacle 之类的功能。

您可以为 Initcap 创建 UDF。

CREATE FUNCTION [dbo].[InitCap] ( @InputString varchar(4000) ) 
RETURNS VARCHAR(4000)
AS
BEGIN

DECLARE @Index          INT
DECLARE @Char           CHAR(1)
DECLARE @PrevChar       CHAR(1)
DECLARE @OutputString   VARCHAR(255)

SET @OutputString = LOWER(@InputString)
SET @Index = 1

WHILE @Index <= LEN(@InputString)
BEGIN
    SET @Char     = SUBSTRING(@InputString, @Index, 1)
    SET @PrevChar = CASE WHEN @Index = 1 THEN ' '
                         ELSE SUBSTRING(@InputString, @Index - 1, 1)
                    END

    IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
    BEGIN
        IF @PrevChar != '''' OR UPPER(@Char) != 'S'
            SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))
    END

    SET @Index = @Index + 1
END

RETURN @OutputString

END
GO

检查 UDF 是否正常工作

select [dbo].[InitCap] ('stackoverflow com');

Stackoverflow Com

你可以像这样更新你的表

update table
set column=[dbo].[InitCap](column);
于 2012-07-27T13:32:04.477 回答
10
update  YourTable
set     company_name = upper(substring(company_name,1,1)) + 
            lower(substring(company_name, 2, len(company_name)-1))
where   len(company_name) > 0

SQL Fiddle 上的实时示例。

于 2012-07-27T12:57:37.883 回答
1

借助像这样的拆分功能。

试试这个,YourTable用你的表名替换:

update T
set Name = P.Name
from YourTable as T
  cross apply (select (select upper(left(X.s, 1))+lower(stuff(X.s, 1, 1, ''))+' '
                       from dbo.split(' ', Name) as X
                       for xml path(''), type).value('.', 'varchar(50)')
              ) as P(Name)
于 2012-07-27T13:11:19.463 回答
1

由于感谢上述帖子,此函数将每个单词的第一个字母大写,除了那些小于特定字符长度的单词,假设为首字母缩略词。如果这没有问题,那么您将第二个参数设置为 0。

CREATE function [dbo].[f_camel_exc_short_words] (@InputString varchar(4000),@AcronymMaxLen INT )
RETURNS VARCHAR(4000)
AS
BEGIN

DECLARE @Index          INT
DECLARE @Char           CHAR(1)
DECLARE @PrevChar       CHAR(1)
DECLARE @Word           VARCHAR(255)
DECLARE @WordChar       CHAR(1)
DECLARE @OutputString   VARCHAR(255)
DECLARE @WordIndex      INT

SET @Word = ''
SET @OutputString = '' 
SET @Index = 1

WHILE @Index <= LEN(@InputString)+1
BEGIN
    SET @Char     = SUBSTRING(@InputString, @Index, 1)
    SET @PrevChar = CASE WHEN @Index = 1 THEN   ' '   ELSE   SUBSTRING(@InputString, @Index - 1, 1)  END

    --IF @Char IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
    --    SET @OutputString = @OutputString + @Char

    IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(','0','1','2','3','4','5','6','7','8','9') or @Index = LEN(@InputString)+1
    BEGIN
         SET @WordIndex = 1
        IF LEN(@Word) > @AcronymMaxLen 
        BEGIN
            WHILE @WordIndex <= LEN(@Word)
            BEGIN
                SET @WordChar = SUBSTRING(@Word,@WordIndex,1)
                if @WordIndex = 1  begin
                    SET @Word = STUFF(@Word,@WordIndex,1,UPPER(@WordChar))    end
                else    begin
                    SET @Word = STUFF(@Word,@WordIndex,1,LOWER(@WordChar))    end
                SET @WordIndex = @WordIndex + 1
            END
        END
        ELSE BEGIN
            SET @Word = UPPER(@Word)
        END
        set @OutputString = @OutputString + @Word
        SET @Word = ''
    END
    SET @Word = @Word + @Char
    SET @Index = @Index + 1
END  

return @OutputString

end
GO

所以PRINT dbo.f_camel_exc_short_words ('PIONEER EURO BOND FUND CLASS C (NON-DIST) (EUR) (OFFSHORE) ISIN LU0119429891',4)返回 Pioneer EURO BOND FUND Class C (NON-Dist) (EUR) (Offshore) ISIN LU0119429891

希望能帮助到你。

于 2013-03-05T20:09:50.727 回答
0

试试这个:

declare @word as nvarchar (50)
set @word = 'ABC COMPANY'
select upper(left(@word, 1)) + lower(SUBSTRING(@word,2,charindex(' ', @word)-2)) + ' ' + 
upper(left(substring(@word,charindex(' ', @word)+1,len(@word)-1),1)) 
+ lower(SUBSTRING(@word,charindex(' ', @word)+2, len(@word)))
于 2014-03-07T16:11:19.090 回答
0

进一步的修改处理所有格 ('s) 和以 Mc 开头的单词

if ' ' + @OutputString like '% Mc%'
set @OutputString = ' ' + @OutputString
set @index = CHARINDEX ( ' Mc', @OutputString)
while @Index > 0
begin
    set @OutputString = SUBSTRING(@outputString, 1, @index + 2) + UPPER(SUBSTRING(@outputString, @index + 3, 1)) + SUBSTRING(@outputString, @index + 4, len(@outputString))
    set @index = CHARINDEX ( ' Mc', @OutputString, @Index + 4)
end
set @outputstring = ltrim(rtrim(@outputstring))

if @OutputString + ' ' like '%''S %' 
set @OutputString = ltrim(rtrim(REPLACE(@outputstring + ' ', '''S ', '''s ')))

就在返回之前

于 2016-07-01T16:21:41.363 回答
-1
CREATE FUNCTION Initcap
( @mystring varchar(50) )
RETURNS VARCHAR(50)
AS
BEGIN
   DECLARE @val VARCHAR(50);
   SET @val = (select upper(left(@mystring,1)) +  lower(substring(@mystring,2,datalength(@mystring)-1)) )
   RETURN @val;
END;
于 2017-01-24T20:07:17.473 回答