sql 实现全角与半角字符转换的处理函数

本文介绍了一个SQL Server中的自定义函数f_Convert,用于将全角字符转换为半角字符或反之。该函数通过Unicode值进行转换,并提供了一个批处理脚本,用于批量更新数据库表中的全角字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--2 实现全角与半角字符转换的处理函数
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
GO

declare T_cursor cursor local for
select
    a.Name,    b.Name
from
    sysobjects a
join
    syscolumns b
on a.ID=b.ID
join
    systypes c
on c.xusertype=b.Xtype
where
    a.xtype
='U' and c.Name in('nvarchar','nchar','varchar','char')
declare @tabName sysname,@ColName sysname
open T_cursor
fetch next from T_cursor into @tabName,@ColName
while @@fetch_status=0
begin
   
exec('update '+@tabName+' set '+@ColName+'=dbo.f_Convert('+@ColName+',0) where PATINDEX(N''%[!-~]%'' COLLATE LATIN1_GENERAL_BIN'+','+@ColName+')>0'
   
fetch next from T_cursor into @tabName,@ColName
end
close T_cursor
deallocate T_cursor

---------------------------------------

--改列的数据全角为半角
declare T_cursor cursor local for
select
    a.Name,    b.Name
from
    sysobjects a
join
    syscolumns b
on a.ID=b.ID
join
    systypes c
on c.xusertype=b.Xtype
where
    a.xtype
='U' and c.Name in('nvarchar','nchar','varchar','char')
declare @tabName sysname,@ColName sysname
open T_cursor
fetch next from T_cursor into @tabName,@ColName
while @@fetch_status=0
begin
   
exec('update '+@tabName+' set '+@ColName+'=dbo.f_Convert('+@ColName+',0) where PATINDEX(N''%[!-~]%'' COLLATE LATIN1_GENERAL_BIN'+','+@ColName+')>0')--少了)
    fetch next from T_cursor into @tabName,@ColName
end
close T_cursor
deallocate T_cursor
go
--改列名全角为半角
declare T_cursor cursor local for
select
    a.Name,    b.Name
from
    sysobjects a
join
    syscolumns b
on a.ID=b.ID
where
    a.xtype
='U' and PATINDEX(N'%[!-~]%' COLLATE LATIN1_GENERAL_BIN,b.Name)>0
declare @tabName sysname,@ColName sysname
open T_cursor
fetch next from T_cursor into @tabName,@ColName
while @@fetch_status=0
begin
   
exec('exe sp_rename '''+@tabName+'.'+@ColName+''',''dbo.f_Convert('+@ColName+',0)''')
   
fetch next from T_cursor into @tabName,@ColName
end
close T_cursor
deallocate T_cursor

------------------------------------------------------
--
改列名全角为半角
declare T_cursor cursor local for
select
    a.Name
+'.'+b.Name,NameNew=dbo.f_Convert(b.Name)
from
    sysobjects a
join
    syscolumns b
on a.ID=b.ID
where
    a.xtype
='U' and PATINDEX(N'%[!-~]%' COLLATE LATIN1_GENERAL_BIN,b.Name)>0
declare @tabName sysname,@ColName sysname
open T_cursor
fetch next from T_cursor into @tabName,@ColName
while @@fetch_status=0
begin
    exe sp_rename
@tabName,@ColName
   
fetch next from T_cursor into @tabName,@ColName
end
close T_cursor
deallocate T_cursor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值