葉子函式分享四十四 全形半形轉換函式

2021-08-25 18:29:38 字數 3988 閱讀 6838

--(此函式部分思路參考了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+007d

0x7e ~u+ff5e ~ u+007e

*/

葉子函式分享四十四 全形半形轉換函式

此函式部分思路參考了 csdn 上大力的轉換函式 鄒建 2005.01 引用請保留此資訊 go 建立函式 create function sbc2dbc str nvarchar 4000 要轉換的字串 flag bit 轉換標誌 0轉換成半形 1轉換成全角 returns nvarchar 400...

葉子函式分享四十六 獲取元素個數的函式

go create function getstrarrlength str varchar 8000 returns intas begin declare int return int declare start int declare next int declare location int...

葉子函式分享四 綜合模糊查詢

建立函式 create function dbo m fuzzyquery v1 str nvarchar 2000 returns nvarchar 2000 as begin declare count int i int declare newchar nvarchar 200 nn nvar...