我想只将sql列中每个句子的每个单词的第一个字母大写.
例如,如果句子是:
‘I like movies’
然后我需要输出:
‘I Like Movies’
查询:
declare @a varchar(15) set @a = 'qWeRtY kEyBoArD' select @a as [Normal text],upper(@a) as [Uppercase text],lower(@a) as [Lowercase text],upper(left(@a,1)) + lower(substring(@a,2,len(@a))) as [Capitalize first letter only]
在这里,我只在我的专栏中做了上,下,首字母大写(这里我只是一个随机的单词).
这是我的结果:
有没有可能做到这一点?
解决方法
declare @a varchar(30); set @a = 'qWeRtY kEyBoArD TEST<>&''"X'; select stuff(( select ' '+upper(left(T3.V,1))+lower(stuff(T3.V,1,'')) from (select cast(replace((select @a as '*' for xml path('')),' ','<X/>') as xml).query('.')) as T1(X) cross apply T1.X.nodes('text()') as T2(X) cross apply (select T2.X.value('.','varchar(30)')) as T3(V) for xml path(''),type ).value('text()[1]','varchar(30)'),'') as [Capitalize first letter only];
这首先通过用空标记< X />替换所有空格将字符串转换为XML.然后它使用nodes()粉碎XML以获得每行一个单词.要将行返回到一个值,它将使用for xml path trick.