Sql 查找大写字符,然后添加空格
我买了一个SQL世界城市/州数据库。在状态数据库中,它将状态名称推到一起。例如:“北卡罗来纳州”或“南卡罗莱纳州” SQL中是否有方法循环并查找大写字符并添加空格Sql 查找大写字符,然后添加空格,sql,uppercase,Sql,Uppercase,我买了一个SQL世界城市/州数据库。在状态数据库中,它将状态名称推到一起。例如:“北卡罗来纳州”或“南卡罗莱纳州” SQL中是否有方法循环并查找大写字符并添加空格 这样“北卡罗莱纳州”就变成了“北卡罗莱纳州”?有几种方法可以实现这一点 使用模式和 为每种情况(例如,REPLACE(在您的示例案例中,状态名称为'hC','hC'))链接最小语句。这似乎是一种攻击,但实际上可能会给您带来最好的性能,因为您的替换集非常小 创建此函数 if object_id('dbo.SpaceBeforeCaps'
这样“北卡罗莱纳州”就变成了“北卡罗莱纳州”?有几种方法可以实现这一点
REPLACE(在您的示例案例中,状态名称为'hC','hC')
)链接最小语句。这似乎是一种攻击,但实际上可能会给您带来最好的性能,因为您的替换集非常小if object_id('dbo.SpaceBeforeCaps') is not null
drop function dbo.SpaceBeforeCaps
GO
create function dbo.SpaceBeforeCaps(@s varchar(100)) returns varchar(100)
as
begin
declare @return varchar(100);
set @return = left(@s,1);
declare @i int;
set @i = 2;
while @i <= len(@s)
begin
if ASCII(substring(@s,@i,1)) between ASCII('A') and ASCII('Z')
set @return = @return + ' ' + substring(@s,@i,1)
else
set @return = @return + substring(@s,@i,1)
set @i = @i + 1;
end;
return @return;
end;
GO
如果绝对无法创建函数,并且需要一次性创建函数,则可以使用递归CTE将字符串拆分(并在需要时同时添加空格),然后使用FOR XML重新组合字符。下面详细说明示例:
-- some sample data
create table #tmp (id int identity primary key, statename varchar(100));
insert #tmp select 'NorthCarolina';
insert #tmp select 'SouthCarolina';
insert #tmp select 'NewSouthWales';
-- the complex query updating the "statename" column in the "#tmp" table
;with cte(id,seq,char,rest) as (
select id,1,cast(left(statename,1) as varchar(2)), stuff(statename,1,1,'')
from #tmp
union all
select id,seq+1,case when ascii(left(rest,1)) between ascii('A') and ascii('Z')
then ' ' else '' end + left(rest,1)
, stuff(rest,1,1,'')
from cte
where rest > ''
), recombined as (
select a.id, (select b.char+''
from cte b
where a.id = b.id
order by b.seq
for xml path, type).value('/','varchar(100)') fixed
from cte a
group by a.id
)
update t
set statename = c.fixed
from #tmp t
join recombined c on c.id = t.id
where statename != c.fixed;
-- check the result
select * from #tmp
----------- -----------
id statename
----------- -----------
1 North Carolina
2 South Carolina
3 New South Wales
就像你如何称呼PATINDEX功能我以前试过PATINDEX。无法让它用于查找大写字母。世界各国…所以它有很多数据。@cha-这是一种语言功能。你会怎么称呼它?不想冒犯你。只是有时候在我们的团队中,我们称bug为“未记录的功能”
-- some sample data
create table #tmp (id int identity primary key, statename varchar(100));
insert #tmp select 'NorthCarolina';
insert #tmp select 'SouthCarolina';
insert #tmp select 'NewSouthWales';
-- the complex query updating the "statename" column in the "#tmp" table
;with cte(id,seq,char,rest) as (
select id,1,cast(left(statename,1) as varchar(2)), stuff(statename,1,1,'')
from #tmp
union all
select id,seq+1,case when ascii(left(rest,1)) between ascii('A') and ascii('Z')
then ' ' else '' end + left(rest,1)
, stuff(rest,1,1,'')
from cte
where rest > ''
), recombined as (
select a.id, (select b.char+''
from cte b
where a.id = b.id
order by b.seq
for xml path, type).value('/','varchar(100)') fixed
from cte a
group by a.id
)
update t
set statename = c.fixed
from #tmp t
join recombined c on c.id = t.id
where statename != c.fixed;
-- check the result
select * from #tmp
----------- -----------
id statename
----------- -----------
1 North Carolina
2 South Carolina
3 New South Wales