當進行資料庫轉換時,早期的 DB 可能都是開 varchar ,有些部份字在 Big5 編碼的時候會以
numeric char 被存入 DB 像 "游席堃" 在 DB就變成 游席堃 ..
當進行資料庫轉換時,早期的 DB 可能都是開 varchar ,有些部份字在 Big5 編碼的時候會以
numeric char 被存入 DB 像 "游席堃" 在 DB就變成 游席堃 ..
下面的 Sql function 主要是提供這需要這樣的轉換作業時使用^^
Create Function fn_nStrToNcr ( @InStr nvarchar(2000) )
Returns nvarchar(2000)
As
Begin
/*轉換nvarchar 到 Ncr */
Declare @TmpStr nvarchar(2000) , @OutStr nvarchar(2000) , @nword nvarchar(1);
Declare @P int;
Set @P = -1;
Set @TmpStr = Convert(nvarchar(2000),Convert(varchar(2000),@InStr));
Set @OutStr = @InStr;
If ( @TmpStr <> @InStr ) Begin
While ( @P <> 0 ) Begin
Set @P = CharIndex('?',@TmpStr,@P+1);
If ( @P = 0 ) Break;
Set @nword = SubString(@InStr,@P,1);
If ( @nword = '?' ) Continue;
Set @OutStr = RePlace(@OutStr,@nword,'&#' + Convert(nvarchar(5),UniCode(@nword)) + ';');
End
End
Return @OutStr;
End
Go
Create Function fn_NcrTonStr ( @InStr nvarchar(2000) )
Returns nvarchar(2000)
As
Begin
/*轉換 ncr 變成 nstr*/
Declare @TmpStr nvarchar(2000) , @NCR nvarchar(6);
Declare @P int;
Set @P = -1;
Set @P = -1;
Set @TmpStr = @InStr;
If ( @InStr Like '%&#[0-9][0-9][0-9][0-9][0-9];%' ) Begin
While ( @P <> 0 And @TmpStr Like '%&#[0-9][0-9][0-9][0-9][0-9];%' ) Begin
Set @P = CharIndex('&#',@InStr,@P+1);
Set @NCR = SubString(@InStr,@P+2,6);
If ( @NCR Like '[0-9][0-9][0-9][0-9][0-9];' ) Begin
Set @TmpStr = RePlace(@TmpStr, '&#' +@NCR , IsNull(nchar(Left(@NCR,5)),'') );
End
End
End
Return @TmpStr;
End
Go
Declare @P varchar(50);
Set @P = '游席堃';
Select @P , dbo.fn_NcrTonStr(@P) , dbo.fn_nStrToNcr( dbo.fn_NcrTonStr(@P))
Returns nvarchar(2000)
As
Begin
/*轉換nvarchar 到 Ncr */
Declare @TmpStr nvarchar(2000) , @OutStr nvarchar(2000) , @nword nvarchar(1);
Declare @P int;
Set @P = -1;
Set @TmpStr = Convert(nvarchar(2000),Convert(varchar(2000),@InStr));
Set @OutStr = @InStr;
If ( @TmpStr <> @InStr ) Begin
While ( @P <> 0 ) Begin
Set @P = CharIndex('?',@TmpStr,@P+1);
If ( @P = 0 ) Break;
Set @nword = SubString(@InStr,@P,1);
If ( @nword = '?' ) Continue;
Set @OutStr = RePlace(@OutStr,@nword,'&#' + Convert(nvarchar(5),UniCode(@nword)) + ';');
End
End
Return @OutStr;
End
Go
Create Function fn_NcrTonStr ( @InStr nvarchar(2000) )
Returns nvarchar(2000)
As
Begin
/*轉換 ncr 變成 nstr*/
Declare @TmpStr nvarchar(2000) , @NCR nvarchar(6);
Declare @P int;
Set @P = -1;
Set @P = -1;
Set @TmpStr = @InStr;
If ( @InStr Like '%&#[0-9][0-9][0-9][0-9][0-9];%' ) Begin
While ( @P <> 0 And @TmpStr Like '%&#[0-9][0-9][0-9][0-9][0-9];%' ) Begin
Set @P = CharIndex('&#',@InStr,@P+1);
Set @NCR = SubString(@InStr,@P+2,6);
If ( @NCR Like '[0-9][0-9][0-9][0-9][0-9];' ) Begin
Set @TmpStr = RePlace(@TmpStr, '&#' +@NCR , IsNull(nchar(Left(@NCR,5)),'') );
End
End
End
Return @TmpStr;
End
Go
Declare @P varchar(50);
Set @P = '游席堃';
Select @P , dbo.fn_NcrTonStr(@P) , dbo.fn_nStrToNcr( dbo.fn_NcrTonStr(@P))