有一个字段是城市名称 CityName, 需要根据大写字母插入空格,比如:
MiddleSackville 改为: Middle Sackville
NanooseBay 改为: Nanoose Bay
OterRioTuey 改为: Oter Rio Tuey
我知道是用: update 表名 set 字段=replace(字段,'字符串','字符串') 这个查询语句,但是如何判断大写字母呢?
求高手给一个答案,有10万条记录,不可能手工去该的。多谢!!!
MiddleSackville 改为: Middle Sackville
NanooseBay 改为: Nanoose Bay
OterRioTuey 改为: Oter Rio Tuey
我知道是用: update 表名 set 字段=replace(字段,'字符串','字符串') 这个查询语句,但是如何判断大写字母呢?
求高手给一个答案,有10万条记录,不可能手工去该的。多谢!!!
/*
----------- -----------
97 65
*/
go
create table [tb]([col] varchar(100))
insert [tb]
select 'MiddleSackville' union all
select 'NanooseBay' union all
select 'OterRioTuey'
go
create function f_space(@s varchar(100))
returns nvarchar(100)
as
begin
declare @i int,@r varchar(100)
set @i=1
set @r=''
while @i<=len(@s)
begin
set @r=@r+
case
when ascii(substring(@s,@i,1))!=ascii(lower(substring(@s,@i,1))) and @i!=1
then ' '+substring(@s,@i,1)
else
substring(@s,@i,1)
end
set @i=@i+1
end
return @r
end
goselect dbo.f_space(col) as col from tb/**
col
-----------------------------
Middle Sackville
Nanoose Bay
Oter Rio Tuey(3 行受影响)
**/--更新使用以下语句(字段长度与函数返回值一致)update tb set col=dbo.f_space(col)