简体   繁体   English

SQL - 将每个单词的首字母大写

[英]SQL - Capitalize first letter of each word

I know that this thread exists all over the place, however, this is a slightly different case. 我知道这个线程遍布整个地方,但是,这是一个稍微不同的情况。 In the suite my company uses, I have limited access to the SQL and cannot run the complicated codes with functions, etc. I had a SQL query that compiled data over multiple columns into one column and used a group by clause to weed out multiplicities. 在我公司使用的套件中,我对SQL的访问权限有限,无法使用函数等运行复杂的代码。我有一个SQL查询,它将多列上的数据编译成一列,并使用group by子句来清除多重性。 However, this caused all the results to be returned in all caps, since variations existed. 但是,这导致所有结果都以全部大写字母返回,因为存在差异。 I must now change it back to how it should be, ie first letter of each word capitalized. 我现在必须将它改回原来的样子,即每个单词的首字母大写。 I need a very concise way of doing this. 我需要一种非常简洁的方法来做到这一点。 The suite uses VBScript and XML, but this particular issue is more complicated because I cannot edit the results on the client-side, the suite simply asks for the column name to be displayed (populate a drop-down menu). 该套件使用VBScript和XML,但这个特殊问题更复杂,因为我无法在客户端编辑结果,套件只是要求显示列名(填充下拉菜单)。 Any suggestions? 有什么建议么? Thanks! 谢谢!

Query: 查询:

Select Insurance 
From
(Select Ins1 as Insurance
From InsAuth2
WHERE Ins1 IS NOT NULL
Union All
Select Ins2 as Insurance
From InsAuth2
WHERE Ins2 IS NOT NULL
Union All
Select Ins3 as Insurance
From InsAuth2
WHERE Ins3 IS NOT NULL
Union All
Select Ins4 as Insurance
From InsAuth2
WHERE Ins4 IS NOT NULL
Union All
Select Ins5 as Insurance
From InsAuth2
WHERE Ins5 IS NOT NULL) as table
Group By Insurance

You did not say that this was for SQL Server but here is a solution in case it is. 你没有说这是针对SQL Server的,但是这是一个解决方案,以防万一。

Here is a working sample. 这是一个工作样本。 Replace the table variable @T with the query you have. 用您的查询替换表变量@T

declare @T table(Insurance varchar(max))

insert into @T values
('BENGT MIKAEL ERIKSSON'),
('XMLCHARTEST<>&''"')

select (
       select upper(T.N.value('.', 'char(1)'))+
                lower(stuff(T.N.value('.', 'varchar(max)'), 1, 1, ''))+' '
       from X.InsXML.nodes('/N') as T(N)
       for xml path(''), type
       ).value('.', 'varchar(max)') as Insurance
from 
  (
  select cast('<N>'+replace(
                    replace(
                    replace(Insurance,
                    '&', '&amp;'),
                    '<', '&lt;'),
                    ' ','</N><N>')+'</N>' as xml) as InsXML
  from @T
  ) as X

Result: 结果:

Insurance
----------------------
Bengt Mikael Eriksson 
Xmlchartest<>&'" 

This will uppercase the first letter of the string. 这将大写字符串的第一个字母。 If there are multiple words, this is better done in the application layer. 如果有多个单词,最好在应用程序层中完成。 You would likely need to write a SQL function to do this. 您可能需要编写一个SQL函数来执行此操作。

select upper(substring(MyColumn, 1, 1)) + substring(MyColumn, 2)
from MyTable

or 要么

select upper(substring(MyColumn, 1, 1)) || substring(MyColumn, 2)
from MyTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM