Ms sql将首字母大写

--辅助表
create table a ( 
a int )
declare @b int
set @b=1
while @b<=30
begin
insert into a values(@b)
set @b=@b+1
end;
go
 
--表数据
declare @a table( a varchar(20),id int)
insert into @a select 'abc',1
insert into @a select 'abc bcd',2
insert into @a select 'abc bcd efg',3
insert into @a select 'abc bcd efg hif',4
 
--结果
select  id,(select a+' ' from 
(select upper(left (substring(ab.a1,a.a,charindex(' ',ab.a1+' ',a.a)-a.a),1))+
right(substring(ab.a1,a.a,charindex(' ',ab.a1+' ',a.a)-a.a),len(substring(ab.a1,a.a,charindex(' ',ab.a1+' ',a.a)-a.a))-1) as a,id
 from a,(select a+' ' as a1,id from @a ) as ab where a.a<=len(ab.a1) and substring(' '+ab.a1,a.a,1)=' ') as a2
where a2.id=a1.id for xml path('')) as a from (select distinct id from @a) a1;
 
id          a
1           Abc 
2           Abc Bcd 
3           Abc Bcd Efg 
4           Abc Bcd Efg Hif
(4 行受影响)

有点复杂了。

 

posted on 2016-11-21 20:28 zhouixi 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/1-Admin/p/6086807.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值