做的是英文站,但数据库中存在一些中文字符,想把这些中文字符全部批量删除或者批量替换,只保留英文字符。
请高手指教。--查询
select * from 表名 where 用户ID like '%[吖-座]%'--删除(一个表,一个列,逐一来删除.)
delete 表名 where 用户ID like '%[吖-座]%'
请高手指教。--查询
select * from 表名 where 用户ID like '%[吖-座]%'--删除(一个表,一个列,逐一来删除.)
delete 表名 where 用户ID like '%[吖-座]%'
请高手指教。--查询
select * from 表名 where 用户ID like '%[吖-座]%'--删除(一个表,一个列,逐一来删除.)
delete 表名 where 用户ID like '%[吖-座]%'-------------------------------------
/*
指定的范围跟排序有关,不指定排序方式,是不对的:
*/
--查询:按拼音排序——“座”不是最后一个汉字,“咗”才是。
select * from 表名 where 用户ID COLLATE Chinese_PRC_CI_AS like '%[吖-咗]%'--查询:按笔画排序
select * from 表名 where 用户ID COLLATE Chinese_PRC_Stroke_CS_AS_KS_WS like '%[一-龘]%'
--删除(一个表,一个列,逐一来删除.)——拼音排序
delete 表名 where 用户ID COLLATE Chinese_PRC_CI_AS like '%[吖-咗]%'--删除(一个表,一个列,逐一来删除.)——笔画排序
delete 表名 where 用户ID COLLATE Chinese_PRC_Stroke_CS_AS_KS_WS like '%[一-龘]%'
做的是英文站,但数据库中存在一些中文字符,想把这些中文字符全部批量删除或者批量替换,只保留英文字符。
*/
--------------------
--测试
declare @test table (c1 varchar(100))
insert @test
select 'a座位b' union all
select '斯b巴c' union all
select 'c旷d考' union all
select '中d国e热敏' union all
select '萨斯ef'--原始数据
select * from @test
/*
a座位b
斯b巴c
c旷d考
中d国e热敏
萨斯ef
*/--替换中文字符
while exists (select 1 from @test where patindex('%[吖-咗]%', c1 COLLATE Chinese_PRC_CI_AS) > 0)
begin
update @test set c1 = stuff(c1, patindex('%[吖-咗]%', c1), 1, '') where patindex('%[吖-咗]%', c1 COLLATE Chinese_PRC_CI_AS) > 0
end--最终数据
select * from @test
/*
ab
bc
cd
de
ef
*/
如果 ASCIII(str) >= 0xA0 就是中文字符 那么就删除或者替换掉。在指定的表的列中删除。
*/--测试
declare @test table (c1 varchar(100))
insert @test
select 'a座位b' union all
select '斯b巴c' union all
select 'c旷d考' union all
select '中d国e热敏' union all
select '萨斯ef'declare @i int,@max int
set @i = 1
select @max = len(c1) from @test
while @i <= @max
begin
while exists (select 1 from @test where len(c1)>=@i and ascii(substring(c1,@i,1))>=0xA0)
update @test set c1 = stuff(c1,@i,1,'') where len(c1)>=@i and ascii(substring(c1,@i,1))>=0xA0
set @i=@i+1
end--最终数据
select * from @test
/*
ab
bc
cd
de
ef
*/
/*
数据类型是ntext
参数数据类型ntext 对于stuff 函数的参数1 无效。
----------------------
不光是stuff,replace函数也无效。
我只知道转换成varchar处理,直接处理text/ntext,期待高手吧。
*/
create table test (c1 ntext)
insert test
select 'a座位b' union all
select '斯b巴c' union all
select 'c旷d考' union all
select '中d国e热敏' union all
select '萨斯ef 'union all
select '吖斯e成功f 咗'
alter table test add id bigint identity(1,1)declare @n bigint,@I bigint
declare @x binary(16)
declare @xi intset @n=@@rowcount
set @i=1while @i<=@n
begin
while exists(select 1 from test where id=@i and patindex('%[吖-咗]%',c1)>0)
begin
select @x=textptr(c1),@xi=patindex('%[吖-咗]%',c1)-1 from test where id=@i
updatetext test.c1 @x @xi 1
end
set @i=@i+1
endalter table test drop column idselect * from test
--result
/*
c1
---------
ab
bc
cd
de
ef
ef (所影响的行数为 6 行)
*/