Home  >  Article  >  Database  >  SqlServer数据库全角和半角互转的方法

SqlServer数据库全角和半角互转的方法

WBOY
WBOYOriginal
2016-06-07 17:43:501179browse

CREATE FUNCTION f_Convert( @str NVARCHAR( 4000 ), -- 要转换的字符串 @flag bit -- 转换标志,0转换成半角,1转换成全角 )RETURNS nvarchar( 4000 ) AS BEGIN DECLARE @pat nvarchar( 8 ),@step int ,@i int ,@spc int IF @flag = 0 SELECT @pat =N,@step=-

CREATE FUNCTION f_Convert( @str NVARCHAR(4000), --要转换的字符串 @flag bit --转换标志,0转换成半角,1转换成全角 )RETURNS nvarchar(4000) AS BEGIN DECLARE @pat nvarchar(8),@step int,@i int,@spc int IF @flag=0 SELECT @pat=N,@step=-65248, @str=REPLACE(@str,N'  ',N' ') ELSE SELECT @pat=N,@step=65248, @str=REPLACE(@str,N' ',N'  ') SET @i=PATINDEX(@pat COLLATE LATIN1_GENERAL_BIN,@str) WHILE @i> 0 SELECT @str=REPLACE(@str, SUBSTRING(@str,@i,1), NCHAR(UNICODE(SUBSTRING(@str,@i,1))+@step)) ,@i=PATINDEX(@pat COLLATE LATIN1_GENERAL_BIN,@str) RETURN(@str) END --table1为要转换的表,美国服务器,香港虚拟主机,column1为要转换的表字段
调用:update table1
set column1=dbo.f_Convert(column1,0); ,香港虚拟主机

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn