如何从字符串(1,2,3,4,5,6)中找到一个数字并将其删除? [英] How to find a number from a string (1,2,3,4,5,6) and remove it ?

查看:322
本文介绍了如何从字符串(1,2,3,4,5,6)中找到一个数字并将其删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,该过程接受一串用逗号分隔的唯一数字

例如:1,2,3,4,5,6,7,8,9,10


现在我想找到一个数字,说 4 并将其从列表中删除.

新列表应类似于 1,2,3,5,6,7,8,9,10


我使用以下方法做到了

I have a stored procedure which accepts a string of unique numbers separated by commas

Ex: 1,2,3,4,5,6,7,8,9,10


Now i want to find a number say 4 and remove it from the list.

New list should look like 1,2,3,5,6,7,8,9,10


I did it using the below method

DECLARE @str varchar(100);

SET @str=','+ '1,2,3,4,5,6,7,8,9,10,11,12';

SELECT STUFF(REPLACE(@str,',12,',''), 1, 1, '');



我正在检查以更好的方式进行操作.
有没有人有更好的主意.请随时回复:).



I am checking to do it in a better way.
Does any one have a better idea.Please feel free to reply :).

推荐答案

您可以删除对STUFF的使用,因为它没有任何作用,因此你只要保持:
You could remove the use of STUFF because it doesn''t do anything, so you just keep:
SELECT REPLACE(@str,'',12,'','''')



祝你好运!



Good luck!


这篇关于如何从字符串(1,2,3,4,5,6)中找到一个数字并将其删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆