ALTER function [dbo].[u_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
begin
select @pat=N‘%[!-~]%‘,@step=-65248,
@str=replace(@str,N‘ ‘,N‘
‘)
end
else
begin
select @pat=N‘%[!-~]%‘,@step=65248,
@str=replace(@str,N‘
‘,N‘ ‘)
end
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
原文:http://www.cnblogs.com/yclnet/p/3580648.html