为了账号安全,请及时绑定邮箱和手机立即绑定

SQL Server:使所有大写字母变为正确的字母/标题字母

SQL Server:使所有大写字母变为正确的字母/标题字母

慕少森 2019-11-11 15:59:41
我有一个作为所有大写字母导入的表,我想将其转换为大写字母。你们使用什么脚本来完成此任务?
查看完整描述

3 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

这是可以解决问题的UDF ...


create function ProperCase(@Text as varchar(8000))

returns varchar(8000)

as

begin

  declare @Reset bit;

  declare @Ret varchar(8000);

  declare @i int;

  declare @c char(1);


  if @Text is null

    return null;


  select @Reset = 1, @i = 1, @Ret = '';


  while (@i <= len(@Text))

    select @c = substring(@Text, @i, 1),

      @Ret = @Ret + 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 @Ret

end

但是,您仍然必须使用它来更新数据。


查看完整回答
反对 回复 2019-11-11
?
慕工程0101907

TA贡献1887条经验 获得超5个赞

我玩游戏有点晚了,但是我相信这是更实用的功能,并且可以与任何语言一起使用,包括俄语,德语,泰语,越南语等。它将在'或-或之后加上大写字母。或(或)或空格(显然是:)。


CREATE FUNCTION [dbo].[fnToProperCase]( @name nvarchar(500) )

RETURNS nvarchar(500)

AS

BEGIN

declare @pos    int = 1

      , @pos2   int


if (@name <> '')--or @name = lower(@name) collate SQL_Latin1_General_CP1_CS_AS or @name = upper(@name) collate SQL_Latin1_General_CP1_CS_AS)

begin

    set @name = lower(rtrim(@name))

    while (1 = 1)

    begin

        set @name = stuff(@name, @pos, 1, upper(substring(@name, @pos, 1)))

        set @pos2 = patindex('%[- ''.)(]%', substring(@name, @pos, 500))

        set @pos += @pos2

        if (isnull(@pos2, 0) = 0 or @pos > len(@name))

            break

    end

end


return @name

END

GO


查看完整回答
反对 回复 2019-11-11
  • 3 回答
  • 0 关注
  • 505 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信