内置函数将每个单词的第一个字母大写
如果 SQL Server 中已经存在这样的函数,我不想为此创建自定义函数。
输入字符串:这是我要转换的字符串
预期输出:这是我要转换的字符串
I don't want to create a custom function for that if such function already exists in SQL Server.
Input string: This is my string to convert
Expected output: This Is My String To Convert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
AFAIK,SQL Server 没有内置的功能。
您必须为其编写自定义函数。
试试这个。
输出:
AFAIK, SQL Server has no built-in function for this.
You have to write custom function for it.
Try this.
Output :
我不得不说“不,那不存在”。这是基于多年来对 T-SQL 中可用字符串函数的研究以及最近在 SQL Server 2008 R2 中进行的一些为期 5 天的课程。
当然,我仍然可能是错的:)。
I'd have to go with "No, that does not exist". This based on several years of perusing the available string-functions in T-SQL and some pretty recent 5-day courses in SQL Server 2008 R2.
Of course, I still could be wrong :).
如果您的操作的目标是美化名称字符串,则正确的大写可以定义为由非字母字符分隔的每个单词的第一个字母。
其他解决方案没有考虑:
Bottomtooth the III”)。
注意:我的解决方案不处理异常。
如果您非常关心这些,那么我建议为这些编写一个 CLR C# 程序集,因为这会很棘手,而字符串是 C# 擅长的领域。
这里的另一个解决方案试图解决这个问题,但它仍然需要“ivan可怕的IV”并输出“**IV***an Terrible The IV*”。
这是我想出的功能:
你可以这样称呼它:
输出将如下所示:
If the goal of your operation is to prettify strings of Names then proper capitalization could be defined as the first letter of each word separated by non-alphabet characters.
Other solutions do not take into account:
Bottomtooth the III").
Note: My solution does not handle exceptions.
If you are very concerned about those, then I suggest writing a CLR C# assembly for those as it will be tricky, and strings are an area where C# excels.
Another solution on here tries to account for this, but it would still take "ivan terrible the iv" and output "**IV***an Terrible The IV*".
This is the function I came up with:
You would call it like so:
The output will look like this:
我的策略
代码
这是我对此的原始帖子:在 SQL Server 中将字符串转换为驼峰式大小写
My Strategy
The Code
Here's my original post on this: Converting String to Camel Case in SQL Server
对于 SQL 2017,该函数可能如下所示:
With SQL 2017 the function could look like this:
像我一样,很多人可能正在寻找查询中的解决方案,查询创建功能,我想出了一种不同的方法:
将 [MessageQueue] 表更改为您自己的表,并将 [Message] 更改为您的字段。
函数 STRING_SPLIT 可能需要将 SQL 兼容性级别增加到 130。
使用外部 REPLACE 函数设置所需的任何分隔符。
Like me, many people may be looking for an in-query solution, query creating function, well I figured out a different approach:
Change [MessageQueue] table for your own table, and [Message] for your field.
The function STRING_SPLIT may require to increase your SQL compatibility level to 130.
Use the outer REPLACE function to set any delimiter you want.
我采用了 @ashish.chotalia 的答案,并将其转换为带有输出参数的临时过程。如果不允许您在生产数据库中创建函数,但您确实需要此功能,这对某人来说可能很方便。
I've taken @ashish.chotalia's answer, and converted it to a temporary procedure with an output parameter. Might be handy for someone if you are not allowed to create functions in production databases, but you do need this functionality.
您可以使用 string_split 和 string_agg 函数来获得所需的输出。请参阅此示例。但是,此方法也会将字符串中的多个空格替换为单个空格。
You can use the string_split and string_agg functions to get the desired output. See this example. However, this approach will also replace multiple spaces with a single space in the string.
这是很简单的事情,不要把它搞复杂。
甲骨文:
SELECT initcap(lower('这是我要转换的字符串')) FROM Dual;
Here is simple thing, don't make it complicated.
Oracle:
SELECT initcap(lower('This is MY striNg to conVerT')) FROM dual;