[英]SQL Server: capitalize first letter of each word without using function
Any suggestions on to how to capitalize only the first letter of each word in a cell with select statement - without using a function? 关于如何使用select语句仅将单元格中每个单词的首字母大写的建议-不使用函数? I do not have permission to create functions/views/tables in the database, so I need to run a query/select statement automated in a batchjob with OGR. 我没有在数据库中创建函数/视图/表的权限,因此我需要使用OGR在批处理作业中自动运行查询/选择语句。
I can only find examples where the first letter of the whole cell is capitalized OR solving the issue with a function. 我只能找到整个单元格的首字母大写或使用函数解决问题的示例。
It concerns a name-column with strings like: 它涉及一个带有以下字符串的名称列:
'JOHN DOE'
'JOHN JANE-DOE'
'JOHN-JANE DOE'
''
'JOHN'
Just for fun 纯娱乐
Declare @YourTable table (ID int,Name varchar(50))
Insert Into @YourTable values
(1,'JOHN DOE'),
(2,'JOHN JANE-DOE'),
(3,'JOHN-JANE DOE'),
(4,''),
(5,'JOHN'),
(6,'PATTY O''BRIAN'),
(7,'OLD MCDONALD')
Declare @Str varchar(max) = (Select ID,Name=Lower(Name) from @YourTable for XML Raw)
Select @Str = Replace(@Str,MapFrom,MapTo)
From (
Select MapFrom=P+C
,MapTo =Upper(P+C)
From (Select * From (Values ('"'),(' '),('-'),(' O'''),(' Mc')) P (P)) A
Cross Join (Select Top 26 C=Char(96+Row_Number() Over (Order By Number)) From master..spt_values) C
Union All
Select * From (Values (' MC',' Mc')) P (MapFrom,MapTo)
) A
Declare @XML xml = cast(@Str as XML)
Select ID = r.value('@ID','int')
,Name = r.value('@Name','varchar(50)')
From @XML.nodes('/row') as A(r)
Cross Apply A.r.nodes('./@*') AS B(attr)
Where attr.value('local-name(.)','varchar(100)') not in ('ID','Active')
Returns 退货
ID Name
1 John Doe
2 John Jane-Doe
3 John-Jane Doe
4
5 John
6 Patty O'Brian
7 Old McDonald
Sorry, I'm new to Stackoverflow and I was not aware of the degree of complexity of this question. 抱歉,我是Stackoverflow的新手,我不知道这个问题的复杂程度。
I found a workaround for now - the company that provides our database infratructure provides an additional table that has the correct formatted names. 我现在找到了一种解决方法-提供我们的数据库基础结构的公司提供了另一个具有正确格式名称的表。 So I've joined the correct formatted names on the table that I'm working with. 因此,我在要使用的表上加入了正确的格式名称。
Thank you for the help. 感谢您的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.