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
  • 相关阅读:
    ElasticSearch基本用法
    几款Http小服务器
    【最长上升子序列】HDU 1087——Super Jumping! Jumping! Jumping!
    方差与样本方差、协方差与样本协方差
    方差与样本方差、协方差与样本协方差
    统计推断(statistical inference)
    统计推断(statistical inference)
    R 语言学习(二)—— 向量
    R 语言学习(二)—— 向量
    R 语言的学习(一)
  • 原文地址:https://www.cnblogs.com/zsy/p/443034.html
Copyright © 2011-2022 走看看