用函数处理字符串(提取/比较)

 


-- 1、如果提取小写字母:

create   function  roy_Number( @name   varchar ( 100 ))
returns   varchar ( 100 )
as
begin

    
declare   @s   varchar ( 100 )

    
while   patindex ( ' %[a-z]% ' , @name  collate Chinese_PRC_BIN) > -- 用二进制排序区分大小写

        
begin

            
set   @s = isnull ( @s , '' ) + substring ( @name , patindex ( ' %[a-z]% ' , @name  collate Chinese_PRC_BIN), 1 )

            
set   @name = stuff ( @name , 1 , patindex ( ' %[a-z]% ' , @name  collate Chinese_PRC_BIN), '' )

        
end

    
return   @s
end

go

-- 测试:

declare   @s   varchar ( 100 )

set   @s = ' CHINA,r~!@#$o%^&*(y,&.;)(*wu^x%i943中8国$@人 '

select  dbo.roy_Number( @s as  显示小写字母


/**/ /*
显示小写字母                                                                                               
---------------------------------------------------------------------------------------------------- 
roywuxi

(所影响的行数为 1 行)

*/


-- drop function roy_Number


                                           

go

-- 2、找出第一个字符串没有在第二个字符串出现的字符

create   function  F_split(
                    
@s1   nvarchar ( 50 ),
                    
@s2   nvarchar ( 50 ),
                    
@split   nvarchar ( 50 ) -- 分隔符
)
returns   nvarchar ( 50 )
as
begin
    
    
declare   @s3   nvarchar ( 50 ), @lenSplit   int
    
    
select   @s3 = '' , @s1 = @s1 + @split , @s2 = @split + @s2 + @split , @lenSplit = len ( @split + ' a ' ) - 2
    
    
while   charindex ( @split , @s1 ) >
    
        
begin
    
            
if   charindex ( @split + left ( @s1 , charindex ( @split , @s1 ) + @lenSplit ), @s2 ) =
    
                
set   @s3 = @s3 + left ( @s1 , charindex ( @split , @s1 ) + @lenSplit )
    
            
set   @s1 = stuff ( @s1 , 1 , charindex ( @split , @s1 ) + @lenSplit , '' )
        
end
    
    
return   left ( @s3 , len ( @s3 ) - 1 )

end

go
declare   @s1   nvarchar ( 50 ), @s2   nvarchar ( 50 )

select   @s1 = ' u,a,b,c,d,e,f ' , @s2 = ' c,b,d,rr '

select  dbo.F_split( @s1 , @s2 , ' , ' as  第一个字符串没有在第二个字符串出现的字符

/**/ /*

第一个字符串没有在第二个字符串出现的字符                               
-------------------------------------------------- 
u,a,e,f

(所影响的行数为 1 行)


*/


-- drop function F_split

原文链接: http://blog.csdn.net/roy_88/article/details/1387459

你可能感兴趣的:(用函数处理字符串(提取/比较))