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

2022-01-31 00:00:00 sql uppercase sql-server title-case

我有一个导入为全大写的表格,我想把它变成正确的大小写.你们用什么脚本来完成这个?

I have a table that was imported as all UPPER CASE and I would like to turn it into Proper Case. What script have any of you used to complete this?

推荐答案

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

Here's a UDF that will do the trick...

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

您仍然需要使用它来更新您的数据.

You will still have to use it to update your data though.

相关文章