首页 > 代码库 > 全角半角转换函数

全角半角转换函数

--(此函数部分思路参考了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 0U+ff10 0 U+0030
0x31 1U+ff11 1 U+0031
0x32 2U+ff12 2 U+0032
0x33 3U+ff13 3 U+0033
0x34 4U+ff14 4 U+0034
0x35 5U+ff15 5 U+0035
0x36 6U+ff16 6 U+0036
0x37 7U+ff17 7 U+0037
0x38 8U+ff18 8 U+0038
0x39 9U+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 AU+ff21 A U+0041
0x42 BU+ff22 B U+0042
0x43 CU+ff23 C U+0043
0x44 DU+ff24 D U+0044
0x45 EU+ff25 E U+0045
0x46 FU+ff26 F U+0046
0x47 GU+ff27 G U+0047
0x48 HU+ff28 H U+0048
0x49 IU+ff29 I U+0049
0x4a JU+ff2a J U+004a
0x4b KU+ff2b K U+004b
0x4c LU+ff2c L U+004c
0x4d MU+ff2d M U+004d
0x4e NU+ff2e N U+004e
0x4f OU+ff2f O U+004f
0x50 PU+ff30 P U+0050
0x51 QU+ff31 Q U+0051
0x52 RU+ff32 R U+0052
0x53 SU+ff33 S U+0053
0x54 TU+ff34 T U+0054
0x55 UU+ff35 U U+0055
0x56 VU+ff36 V U+0056
0x57 WU+ff37 W U+0057
0x58 XU+ff38 X U+0058
0x59 YU+ff39 Y U+0059
0x5a ZU+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 aU+ff41 a U+0061
0x62 bU+ff42 b U+0062
0x63 cU+ff43 c U+0063
0x64 dU+ff44 d U+0064
0x65 eU+ff45 e U+0065
0x66 fU+ff46 f U+0066
0x67 gU+ff47 g U+0067
0x68 hU+ff48 h U+0068
0x69 iU+ff49 i U+0069
0x6a jU+ff4a j U+006a
0x6b kU+ff4b k U+006b
0x6c lU+ff4c l U+006c
0x6d mU+ff4d m U+006d
0x6e nU+ff4e n U+006e
0x6f oU+ff4f o U+006f
0x70 pU+ff50 p U+0070
0x71 qU+ff51 q U+0071
0x72 rU+ff52 r U+0072
0x73 sU+ff53 s U+0073
0x74 tU+ff54 t U+0074
0x75 uU+ff55 u U+0075
0x76 vU+ff56 v U+0076
0x77 wU+ff57 w U+0077
0x78 xU+ff58 x U+0078
0x79 yU+ff59 y U+0079
0x7a zU+ff5a z U+007a
0x7b {U+ff5b { U+007b
0x7c |U+ff5c | U+007c
0x7d }U+ff5d } U+007d
0x7e ~U+ff5e ~ U+007e
*/

 

全角半角转换函数