sqlServer去除欄位中的中文

來源:互聯網
上載者:User

標籤: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去除欄位中的中文

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.