【叶子函数分享四十四】全角半角转换函数

--(此函数部分思路参考了CSDN上大力的转换函数)

--邹建2005.01(引用请保留此信息)--*/

go

--创建函数

create function SBC2DBC

(

@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

--测试示例

select dbo.SBC2DBC('~~~~ca!b',1)

--运行结果

/*

~~~~ca!b

*/

--附半角全角表

/*

ASCII 全角字符 Unicode 半角字符 Unicode

0x20 " "空格U+3000 " "空格U+0020

0x21 U+ff01 ! U+0021

0x22 U+ff02 " U+0022

0x23 U+ff03 # U+0023

0x24 U+ff04 $ U+0024

0x25 U+ff05 % U+0025

0x26 U+ff06 & U+0026

0x27 U+ff07 ' U+0027

0x28 U+ff08 ( U+0028

0x29 U+ff09 ) U+0029

0x2a U+ff0a * U+002a

0x2b U+ff0b + U+002b

0x2c U+ff0c , U+002c

0x2d U+ff0d - U+002d

0x2e U+ff0e . U+002e

0x2f U+ff0f / U+002f

0x30 U+ff10 0 U+0030

0x31 U+ff11 1 U+0031

0x32 U+ff12 2 U+0032

0x33 U+ff13 3 U+0033

0x34 U+ff14 4 U+0034

0x35 U+ff15 5 U+0035

0x36 U+ff16 6 U+0036

0x37 U+ff17 7 U+0037

0x38 U+ff18 8 U+0038

0x39 U+ff19 9 U+0039

0x3a U+ff1a : U+003a

0x3b U+ff1b ; U+003b

0x3c U+ff1c < U+003c

0x3d U+ff1d = U+003d

0x3e U+ff1e > U+003e

0x3f U+ff1f ? U+003f

0x40 U+ff20 @ U+0040

0x41 U+ff21 A U+0041

0x42 U+ff22 B U+0042

0x43 U+ff23 C U+0043

0x44 U+ff24 D U+0044

0x45 U+ff25 E U+0045

0x46 U+ff26 F U+0046

0x47 U+ff27 G U+0047

0x48 U+ff28 H U+0048

0x49 U+ff29 I U+0049

0x4a U+ff2a J U+004a

0x4b U+ff2b K U+004b

0x4c U+ff2c L U+004c

0x4d U+ff2d M U+004d

0x4e U+ff2e N U+004e

0x4f U+ff2f O U+004f

0x50 U+ff30 P U+0050

0x51 U+ff31 Q U+0051

0x52 U+ff32 R U+0052

0x53 U+ff33 S U+0053

0x54 U+ff34 T U+0054

0x55 U+ff35 U U+0055

0x56 U+ff36 V U+0056

0x57 U+ff37 W U+0057

0x58 U+ff38 X U+0058

0x59 U+ff39 Y U+0059

0x5a U+ff3a Z U+005a

0x5b U+ff3b [ U+005b

0x5c U+ff3c / U+005c

0x5d U+ff3d ] U+005d

0x5e U+ff3e ^ U+005e

0x5f _U+ff3f _ U+005f

0x60 U+ff40 ` U+0060

0x61 U+ff41 a U+0061

0x62 U+ff42 b U+0062

0x63 U+ff43 c U+0063

0x64 U+ff44 d U+0064

0x65 U+ff45 e U+0065

0x66 U+ff46 f U+0066

0x67 U+ff47 g U+0067

0x68 U+ff48 h U+0068

0x69 U+ff49 i U+0069

0x6a U+ff4a j U+006a

0x6b U+ff4b k U+006b

0x6c U+ff4c l U+006c

0x6d U+ff4d m U+006d

0x6e U+ff4e n U+006e

0x6f U+ff4f o U+006f

0x70 U+ff50 p U+0070

0x71 U+ff51 q U+0071

0x72 U+ff52 r U+0072

0x73 U+ff53 s U+0073

0x74 U+ff54 t U+0074

0x75 U+ff55 u U+0075

0x76 U+ff56 v U+0076

0x77 U+ff57 w U+0077

0x78 U+ff58 x U+0078

0x79 U+ff59 y U+0079

0x7a U+ff5a z U+007a

0x7b U+ff5b { U+007b

0x7c U+ff5c | U+007c

0x7d U+ff5d } U+007d

0x7e U+ff5e ~ U+007e

*/

你可能感兴趣的:(函数)