在论坛中出现的比较难的sql问题:13(循环替换问题 过滤各种标点符号)

空扰寡人 提交于 2019-12-11 00:17:44
原文:在论坛中出现的比较难的sql问题:13(循环替换问题 过滤各种标点符号)

所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。


去掉一个字段中的标点符号的SQL语句怎么写

http://bbs.csdn.net/topics/390621077?page=1#post-395850514

比如有一个字段    题名
         1       水尼“十万”个为什么
         2      当代工人:市场化的演变与趋势
         3      当代画家  (东北卷)

想把这个字段中的标点符号去掉,请教各位大侠SQL 语句该怎么写。

我的解法是,通过新建一个符号表,里面存储要替换的各种符号,然后通过循环,把这些符号替换了,

如果数据多了,应该效率不太好:

if object_id('t') is not null   drop table tgo create table t(id int,title nvarchar(100)) insert into tselect 1,'水尼“十万”个为什么' union allselect 2,'当代工人:市场化的演变与趋势' union allselect 3,'当代画家(东北卷)' union allselect 4,'当代画家:“北京篇:;”'  if object_id('symbol') is not null   drop table symbolgo --建立一个标点符号的表,你可以往里面加各种你想替换的标点符号create table symbol (n nvarchar(10)); insert into symbolselect '“' union allselect '”' union allselect ':' union allselect ';' go  if exists(select * from sys.objects where name = 'fn_replace_symbol')   drop function dbo.fn_replace_symbol;go create function dbo.fn_replace_symbol(@n nvarchar(1000))returns nvarchar(1000)asbegindeclare @i int;declare @count int; set @i = 1set @count = (select count(*) from symbol);       while @i <= @countbegin   ;with t   as   (   select n,          row_number() over(order by @@servername) as rownum   from symbol   )       select @n = replace(@n,(select n from t where rownum = @i),'')   set @i = @i + 1end return @nendgo --替换效果select * ,       dbo.fn_replace_symbol(title) as 替换完后的字符from t/*id	title	                      替换完后的字符1	水尼“十万”个为什么	           水尼十万个为什么2	当代工人:市场化的演变与趋势	  当代工人市场化的演变与趋势3	当代画家(东北卷)	           当代画家(东北卷)4	当代画家:“北京篇:;”	      当代画家北京篇*/


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!