Microsoft SQL Server 全角转半角函数

先创建函数,函数如下

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

使用方法如下(修改为半角,修改为全角参数设为1):

UPDATE your_table SET your_field= dbo.f_Convert(your_field,0)

转载于:https://www.cnblogs.com/tecorey/p/7649439.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在MySQL中,要实现全角半角的功能,可以使用自定义函数来完成。下面是一个简单的示例: 首先,创建一个自定义函数`fullwidth_to_halfwidth`来实现全角半角转换逻辑: DELIMITER $$ CREATE FUNCTION fullwidth_to_halfwidth(src CHAR(255)) RETURNS CHAR(255) BEGIN DECLARE result CHAR(255); SET result = REPLACE(src, '0', '0'); SET result = REPLACE(result, '1', '1'); SET result = REPLACE(result, '2', '2'); SET result = REPLACE(result, '3', '3'); SET result = REPLACE(result, '4', '4'); SET result = REPLACE(result, '5', '5'); SET result = REPLACE(result, '6', '6'); SET result = REPLACE(result, '7', '7'); SET result = REPLACE(result, '8', '8'); SET result = REPLACE(result, '9', '9'); SET result = REPLACE(result, 'A', 'A'); SET result = REPLACE(result, 'B', 'B'); SET result = REPLACE(result, 'C', 'C'); SET result = REPLACE(result, 'D', 'D'); SET result = REPLACE(result, 'E', 'E'); SET result = REPLACE(result, 'F', 'F'); SET result = REPLACE(result, 'G', 'G'); SET result = REPLACE(result, 'H', 'H'); SET result = REPLACE(result, 'I', 'I'); SET result = REPLACE(result, 'J', 'J'); SET result = REPLACE(result, 'K', 'K'); SET result = REPLACE(result, 'L', 'L'); SET result = REPLACE(result, 'M', 'M'); SET result = REPLACE(result, 'N', 'N'); SET result = REPLACE(result, 'O', 'O'); SET result = REPLACE(result, 'P', 'P'); SET result = REPLACE(result, 'Q', 'Q'); SET result = REPLACE(result, 'R', 'R'); SET result = REPLACE(result, 'S', 'S'); SET result = REPLACE(result, 'T', 'T'); SET result = REPLACE(result, 'U', 'U'); SET result = REPLACE(result, 'V', 'V'); SET result = REPLACE(result, 'W', 'W'); SET result = REPLACE(result, 'X', 'X'); SET result = REPLACE(result, 'Y', 'Y'); SET result = REPLACE(result, 'Z', 'Z'); SET result = REPLACE(result, 'a', 'a'); SET result = REPLACE(result, 'b', 'b'); SET result = REPLACE(result, 'c', 'c'); SET result = REPLACE(result, 'd', 'd'); SET result = REPLACE(result, 'e', 'e'); SET result = REPLACE(result, 'f', 'f'); SET result = REPLACE(result, 'g', 'g'); SET result = REPLACE(result, 'h', 'h'); SET result = REPLACE(result, 'i', 'i'); SET result = REPLACE(result, 'j', 'j'); SET result = REPLACE(result, 'k', 'k'); SET result = REPLACE(result, 'l', 'l'); SET result = REPLACE(result, 'm', 'm'); SET result = REPLACE(result, 'n', 'n'); SET result = REPLACE(result, 'o', 'o'); SET result = REPLACE(result, 'p', 'p'); SET result = REPLACE(result, 'q', 'q'); SET result = REPLACE(result, 'r', 'r'); SET result = REPLACE(result, 's', 's'); SET result = REPLACE(result, 't', 't'); SET result = REPLACE(result, 'u', 'u'); SET result = REPLACE(result, 'v', 'v'); SET result = REPLACE(result, 'w', 'w'); SET result = REPLACE(result, 'x', 'x'); SET result = REPLACE(result, 'y', 'y'); SET result = REPLACE(result, 'z', 'z'); RETURN result; END$$ DELIMITER ; 然后就可以在SQL语句中调用这个函数来执行全角半角的操作。 例如,假如有一个名为`contacts`的表,其中有一个名为`name`的字段,需要将该字段内的全角字符转换半角: SELECT fullwidth_to_halfwidth(name) FROM contacts; 以上示例代码会将`contacts`表中`name`字段内的全角字符半角并返回结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值