SQL Server 中的聚合替换?
我想要实现的目标是使必须在某个字段上执行的一系列替换动态化。 (为了让事情变得更容易,我实际上想要删除数据,所以我会一直与
说有时我只需要进行一次替换:
... REPLACE(myField, stringToRemove, '')
有时,我需要两次替换:
... REPLACE(REPLACE(myField, stringToRemove, ''), anotherStringToRemove, '')
但是,我需要这样做动态的,我事先不知道我将拥有多少个值,因此,我需要进行多少次替换(删除),
我尝试搜索聚合字符串操作函数,当然,没有。我也知道这可以通过以下方式实现 但我无法使用它。
CLR 聚合函数,
What I'm trying to achieve is to make dynamic a series of replacements that have to be performed on a certain field. (To make things even easier, I want in fact to remove data, so I'll be always comparing with
Say that sometimes I will have to do just one replacement:
... REPLACE(myField, stringToRemove, '')
Sometimes, I will need two replacements:
... REPLACE(REPLACE(myField, stringToRemove, ''), anotherStringToRemove, '')
However, I need to make this dynamic and I do not know in advance how many of those values I'll have, and so, how many replacements (removals) I'll have to do.
I tried searching for aggregate string manipulation functions and, of course, there's none. I also know that this can be achieved through a CLR aggregate function but I don't have the possibility of using it.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 FromValue 和 ToValue 设置表变量,并使用 while 循环进行替换。
结果:
如果您只想查询值,您可以执行以下操作。
You can setup a table variable with FromValue and ToValue and use a while loop to do the replacements.
Result:
If you only want to query the values you can do something like this.
一旦实现了下面的 CLR 聚合函数,您就可以执行以下操作:
C# 中的 CLR 聚合函数
Once you implement the CLR aggregate function below, you can do:
CLR aggregate function in C#
您可能必须编写一个标量函数,向其中传递原始字符串和足够的信息以使其知道要删除哪些字符串,并让它循环遍历它们并返回替换集的结果。
You might have to write a scalar function to which you pass the original string, and enough information for it to know which strings to remove, and have it loop through them and return the result of the set of replacements.