简体   繁体   English

SQL Server:不使用函数将每个单词的首字母大写

[英]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.

相关问题 SQL服务器中字符串中每个单词的首字母如何大写 - How to capitalize the first letter of each word in a string in SQL Server 在选择语句SQL Server中将每个单词的首字母大写 - Capitalize first letter of each word in select statement SQL Server SQL - 将每个单词的首字母大写 - SQL - Capitalize first letter of each word 每个单词首字母大写,不影响连续字母 - Capitalize the first letter of each word without affecting the consecutive letters SQL - 仅大写每个单词的第一个字母,小写所有 rest - SQL - Capitalize only the first letter of each word and lowercase all the rest MySQL:触发以大写每个单词的第一个字母 - MySQL : trigger to capitalize first letter of each word 大写第一个字母,每个单词,修复可能的字符串拆分顺序问题,T-SQL 2017 中的多个分隔符,而不使用用户定义的函数 - Capitalize first letter, every word, fix possible string split order issues, multiple delimiters in T-SQL 2017 without using a user-defined function MYSQL-将每个句子中第一个单词的首字母大写 - MYSQL - Capitalize the first letter of the first word in each sentence SQL 首字母大写,除3个字母的单词 - SQL Capitalize first letter except 3 letter words 选择 SQL 中字符串中每个单词的首字母 - Selecting first letter of each word in a string in SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM