纸上得来终觉浅,绝知此事要躬行。

 

SQL Server标量值函数-汉字转拼音

/****** 对象:  UserDefinedFunction [dbo].[HZ2PY]    脚本日期: 05/02/2012 18:23:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

--汉字转换拼音函数
ALTER Function [dbo].[HZ2PY](     @cString nVarChar (200)) Returns nVarChar(100)
AS 
Begin
    Declare @nFor    Int      --字符串循环用 
    Declare @nPYFor  Int      --拼音串循环用 
    Declare @cPYString  VarChar(100)     --拼音串 
    Declare @cCurChar VarChar(2)         --当前字符  
    Declare @vReturn VarChar (100)      ----将返回的结果 
    Set @cPYString = '吖八嚓咑妸发旮铪讥讥咔垃呣拿讴趴七呥仨他哇哇哇夕丫匝咗' 
    Set @vReturn = ''  
    Set @nFor = 0  
    While @nFor < Len(@cString)   
    Begin   
        Set @nFor = @nFor + 1   
        Set @cCurChar = Substring(@cString,@nFor,1)   
        Set @nPYFor = 0   
        While @nPYFor < len(@cPYString)      
        Begin      
            Set @nPYFor = @nPYFor + 1  
            If @cCurChar < Substring(@cPYString,@nPYFor,1)   
                Begin         
                    Set @vReturn = Rtrim(@vReturn) + (Case When @nPYFor <> 1 Then Char(63 + @nPYFor) Else @cCurChar End)   
                    Break        
                End     
            Else     
                Begin   
                    Continue 
                End     
        End    
    End 
    Return @vReturn
END

posted on 2012-05-02 18:28  JRoger  阅读(475)  评论(0编辑  收藏  举报

导航