您的位置:首页 > 博客中心 > 数据库 >

SQL Server 中LEN函数的问题

时间:2022-03-14 03:29

LEN(‘T ‘) =1

LEN(‘ T‘) =2

在数据库中分解字符串时要注意,例如以‘^‘分隔‘X ^ T ‘,分解时要注意最后的‘T ‘被分解成‘T‘

可用如下的代码来进行完整的分解

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[FnsplitWithEmpty](@SourceStr varchar(8000),@StrSeprate varchar(10))
returns @temp table(id int, strList varchar(1000))
as 
begin
    declare @i int
    --set @SourceStr = rtrim(ltrim(@SourceStr))
    set @i = charindex(@StrSeprate,@SourceStr)
    declare @j int
    set @j = 0
    while @i>=1
    begin
        insert @temp values(@j,left(@SourceStr,@i-1))
        set @SourceStr = substring(@SourceStr,@i+1,len(@SourceStr + ‘x‘) - 1 -@i)
        set @i = charindex(@StrSeprate,@SourceStr)
        set @j = @j + 1
    end
    if @SourceStr <> ‘‘
       insert @temp values(@j,@SourceStr)
    return 
end

 

热门排行

今日推荐

热门手游