zoukankan      html  css  js  c++  java
  • 数据库截取一定长度的字符串

    数据库截取一定长度的字符串:

    /*******************************************

        作者:小朱
        功能:获取一定长度的字符串
        日期:2004年11月01日

    ******************************************
    */

    CREATE FUNCTION [dbo].[uf_GetString]
        (
            
    @str VarChar(2000= '',    --要截取的字符串
            @getLen Int = 0            --要截取的长度,按中文的汉字计算
        )  
        
    RETURNS VarChar(2000AS  
    BEGIN 
        
    Declare @lastStr VarChar(2000)
        
    Declare @tempStr VarChar(2000)
        
    Declare @str1 VarChar(2)
        
    Declare @pos Int
        
    Declare @ChineseCount Int
        
    Declare @EnglishCount Int
        
    Select @ChineseCount = 0
        
    Select @EnglishCount = 0
        
    Select @pos = 1
        
    Select @tempStr = LTrim(RTrim(@str))
        
    While @EnglishCount / 2 + @ChineseCount < @getLen
        
    Begin
            
    If Len(@tempStr< @getLen  OR @pos + 1 > Len(@tempStr
            
    Begin
                
    Select @lastStr = @tempStr
                
    Break
            
    End
            
    Else
            
    Begin
                
    Select @str1 = SubString(@tempStr,@pos,1)
                
    If DataLength(@str1= Len(@str1)
                    
    Select @EnglishCount = @EnglishCount + 1
                
    Else
                    
    Select @ChineseCount = @ChineseCount + 1
                
                
    If @EnglishCount / 2 + @ChineseCount >= @getLen
                
    Begin
                    
    If @EnglishCount % 2 <> 0
                        
    Select @lastStr = SubString(@tempStr,1,@pos -1+ ''
                    
    Else
                        
    Select @lastStr = SubString(@tempStr,1,@pos+ ''
                    
    Break
                
    End
                
    Select @pos = @pos + 1
            
    End
        
    End
        
    Return @lastStr 
    END
  • 相关阅读:
    MessageFormat使用记录
    在IDEA中使用MyBatis Generator逆向工程生成代码
    mybatis报错invalid types () or values ()解决方法
    关于jrebel碰到的一次问题记录
    I/O限制异步操作
    线程基础和异步执行计算限制
    模板方法模式(Head first 设计模式——7)
    适配器模式和外观模式(head first设计模式——6)
    命令模式(head first 设计模式5)
    工厂模式——(Head first设计模式4)
  • 原文地址:https://www.cnblogs.com/zsy/p/443034.html
Copyright © 2011-2022 走看看