替换逗号分隔列表中的特定数字
我正在尝试使用 SQL Server 中的 REPLACE 函数替换数字组合,但替换特定行中的特定数字组合。例如: row value = '17,171,217,317,28' 并将这样做:
UPDATE TABLENAME SET COLUMN_NAME = REPLACE(column value,'17,','')
但这将替换在列行中找到的所有 17 个。
如何指定仅替换行中找到的开头 17 个?
I'm trying to replace a combination of numbers using the REPLACE
function in SQL Server but a specific ones inside a specific row. For example:
row value = '17,171,217,317,28' and will do it like this:
UPDATE TABLENAME SET COLUMN_NAME = REPLACE(column value,'17,','')
but that will replace all the 17 found it in the column row.
How do I specify to only replace the starting 17 found in the row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用这个:
请注意,我同意不这样存储您的数据,违反了 1NF(第一范式)
Try using this:
Note that I agree not to store your data like that, violates 1NF (First Normal Form)
如果我正确理解了要求:
If I understand the requirement correctly:
如果您可以修复数据库设计以不使用逗号分隔列表,那么这是最好的解决方案。如果这不是一个选择,则以下方法可能有效。
我假设您想要删除列表开头、中间或结尾处的整个值,例如“17,171,217,317,28”、“13,17,19”或“11,13,17”。如果相同的值多次存在,则应将其全部删除。示例:“17,23,17,217,17”将变为“23,217”。
这可以通过在开始/结束处添加额外的分隔符、用单个分隔符替换分隔值(分隔符 + 值 + 分隔符),然后删除刚刚添加的额外分隔符来完成。如果可能存在重复的连续值,则现有分隔符将需要暂时加倍,并且替换将更改为仅删除任何匹配的(分隔符 + 值 + 分隔符)序列。
处理所有边缘情况的逻辑可能会变得复杂。以下内容对于各种测试数据似乎相当稳健。
结果:
TRIM(char FROM string)
语法在 SQL Server 2019 及更高版本中可用。对于旧版本,修剪前导和尾随分隔符会变得很棘手。一种方法涉及 STUFF/REVERSE/STUFF/REVERSE 序列,需要处理空结果的特殊情况。请参阅此 db<>fiddle 进行演示。
If you can fix your database design to not use comma separated lists, that is the best solution. If that is not an option, the following approach may work.
I assume that you want to remove whole values at either the start, middle, or end of the list, such as "17,171,217,317,28", "13,17,19", or "11,13,17". If the same value exists more than once, all should be removed. Example: "17,23,17,217,17" would become "23,217".
This can be done by adding extra delimiters at the start/end, replacing the delimited value (delimiter + value + delimiter) with a single delimiter, and then stripping the extra delimiters just added. If there is a chance that duplicate consecutive values may exist, existing delimiters will need to be temporarily doubled up and the replace would change to simply remove any matching (delimiter + value + delimiter) sequance.
Logic to handle all of the edge cases can get complex. The following appears to be fairly robust over a variety of test data.
Results:
The
TRIM(char FROM string)
syntax is available in SQL Server 2019 and later. For older versions, trimming leading and trailing delimiters gets tricky. One approach involves a STUFF/REVERSE/STUFF/REVERSE sequence, with a special case needed to handle an empty result.See this db<>fiddle for a demo.
如果保留顺序并不重要,则可以使用 STRING_SPLIT() 和 STRING_AGG()。
请参阅此 db<>fiddle 演示。
警告。尽管示例倾向于保留小列表中的顺序,但函数文档中并不能保证这一点。这些函数仅在 SQL Server 2017 及更高版本中可用。
If preserving order does not matter, you can use STRING_SPLIT() and STRING_AGG().
See this db<>fiddle fr a demo.
Caution. Although The examples tend to preserve order in small lists, this is not guaranteed in the function documentation. These functions are only available in SQL Server 2017 and later.