所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。
1、去掉一个字段中的标点符号的SQL语句怎么写
http://bbs.csdn.net/topics/390621077?page=1#post-395850514
比如有一个字段 题名
1 水尼“十万”个为什么
2 当代工人:市场化的演变与趋势
3 当代画家 (东北卷)
想把这个字段中的标点符号去掉,请教各位大侠SQL 语句该怎么写。
我的解法是,通过新建一个符号表,里面存储要替换的各种符号,然后通过循环,把这些符号替换了,
如果数据多了,应该效率不太好:
if object_id('t') is not null drop table t go create table t(id int,title nvarchar(100)) insert into t select 1,'水尼“十万”个为什么' union all select 2,'当代工人:市场化的演变与趋势' union all select 3,'当代画家(东北卷)' union all select 4,'当代画家:“北京篇:;”' if object_id('symbol') is not null drop table symbol go --建立一个标点符号的表,你可以往里面加各种你想替换的标点符号 create table symbol (n nvarchar(10)); insert into symbol select '“' union all select '”' union all select ':' union all select ';' 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) as begin declare @i int; declare @count int; set @i = 1 set @count = (select count(*) from symbol); while @i <= @count begin ;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 + 1 end return @n end go --替换效果 select * , dbo.fn_replace_symbol(title) as 替换完后的字符 from t /* id title 替换完后的字符 1 水尼“十万”个为什么 水尼十万个为什么 2 当代工人:市场化的演变与趋势 当代工人市场化的演变与趋势 3 当代画家(东北卷) 当代画家(东北卷) 4 当代画家:“北京篇:;” 当代画家北京篇 */
2、为什么replace函数替换不了 ,请高手指点:http://bbs.csdn.net/topics/390625259
delete_author表中的内容是:
我写成一个存储过程:(有些输出是为了测试)
create proc proc_del_special
@input_str varchar(100),@output_str varchar(100) output
as
declare @author varchar(256),@delsym varchar(10),@temp_str varchar(100)
declare del_cur scroll cursor for select del_author from delete_author
open del_cur
fetch next from del_cur into @delsym
set @author=@input_str
while @@fetch_status=0
begin
print replace(@author,@delsym,'')
set @temp_str=replace(@author,@delsym,'')
set @author=@temp_str
print @author
print @delsym
fetch next from del_cur into @delsym
end
set @output_str=@author
close del_cur
deallocate del_cur
测试例子:
declare @a1 varchar(100),@a2 varchar(100)
set @a1='(美)啊那编译'
exec proc_del_special @a1,@a2 output
select @a2
执行结果还是:(美)啊那编译
执行过程中的消息:
美)啊那编译
(美)啊那编译
主编
(美)啊那编译
(美)啊那编译
编译
(美)啊那编译
(美)啊那编译
(美)
(美)啊那编译
(美)啊那编译
(英)
(美)啊那编译
(美)啊那编译
(苏)
(美)啊那编译
(美)啊那编译
[等]
(美)啊那编译
(美)啊那编译
(日)
(美)啊那编译
(美)啊那编译
编著
(美)啊那编译
(美)啊那编译
(西)
(1 行受影响)
就是replace函数没有替换成功,这是为什么啊 ,请哪位大侠指点一下 ,在线等,非常感谢了
--drop table tb create table tb(v nvarchar(100)) insert into tb select '(英)啊那编译' union all select '小泉纯一郎(日)' union all select '皇家马德里对(西)主编' union all select '奥巴马(美)编著' union all select '中国[等]' create table delete_author (del_author nvarchar(50)) insert into delete_author select '主编' union all select '编译' union all select '(美)' union all select '(英)' union all select '(苏)' union all select '[等]' union all select '(日)' union all select '编著' union all select '(西)' 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) as begin declare @i int; declare @count int; set @i = 1 set @count = (select count(*) from delete_author); while @i <= @count begin ;with t as ( select del_author, row_number() over(order by @@servername) as rownum from delete_author ) select @n = replace(@n,(select del_author from t where rownum = @i),'') set @i = @i + 1 end return @n end go --替换效果 select * , dbo.fn_replace_symbol(v) as 替换完后的字符 from tb /* v 替换完后的字符 (英)啊那编译 啊那 小泉纯一郎(日) 小泉纯一郎 皇家马德里对(西)主编 皇家马德里对 奥巴马(美)编著 奥巴马 中国[等] 中国 */