【043】多语言表字符串处理fn_GetCombineName
侧边栏壁纸
  • 累计撰写 60 篇文章
  • 累计收到 2 条评论

【043】多语言表字符串处理fn_GetCombineName

秋驰雪隙
2025-04-08 / 0 评论 / 10 阅读 / 正在检测是否收录...
if(OBJECT_ID('fn_GetCombineName') is not null)
    drop function fn_GetCombineName
go

CREATE FUNCTION [dbo].[fn_GetSegName] 
(
    @CombineName nvarchar(max),
    @SplitStr nvarchar(50),
    @SegIndex int
)
RETURNS nvarchar(max)
AS
BEGIN
    
    --如果组合名称自身为空,则返回空串
    if (@CombineName is null or @CombineName='') return ''
    
    declare @Result nvarchar(max)
    
    declare @x nvarchar(max)
    select @x=@CombineName    
    select @x=replace(@x,' ',' ')
    select @x=replace(@x,'',' ')

    declare @curIndex int
    
    declare @i int
    set @i=1
    WHILE @i<=@SegIndex
    BEGIN
        set @curIndex=charIndex(@SplitStr,@x,0)
        if @curIndex=0
        BEGIN
            if (@i=@SegIndex) set @result=@x else  set @result=''
            break
        END
        set @result=left(@x,@curIndex-1)
        if (@i=@SegIndex) break
        
        --准备下次循环
        set @x=right(@x,len(@x)-len(@result)-len(@SplitStr))
        set @i=@i+1
    END
    return @Result
END
0

评论 (0)

取消