標籤:end ide alt bsp identity arch data 組成 方法
很多時候資料庫表中某些欄位是由中文和字母或數字組成,但有時我們又需要將欄位中的中文去掉。想要實現這種需求的方法有很多,下面就是其中一種解決方案。
首先我們先建立測試資料
create table test(id int primary key identity(1,1),name varchar(20) not null)insert into test(name) values(‘測試2‘)insert into test(name) values(‘測試a‘)insert into test(name) values(‘測試‘)insert into test(name) values(‘abc‘)insert into test(name) values(‘123‘)insert into test(name) values(‘abc123‘)select * from test
結果:
建立函數:
--去除輸入字串中的中文create function fun_del_chinese(@col varchar(1000))returns varchar(1000)ASbegin declare @returnchar varchar(1000),@len int select @returnchar=‘‘,@len=1 while(@len<=len(@col)) begin if(ASCII(substring(@col,@len,1))<122) set @[email protected]+substring(@col,@len,1) set @[email protected]+1 endreturn @returncharendgo
執行:
update test set name=t2.namefrom test t1,(select id,dbo.fun_del_chinese(name) name from testwhere len(name)*2!=datalength(name)--排除全部由中文組成欄位) t2where t1.id=t2.id
執行結果:
sqlServer去除欄位中的中文