gate_s

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

 

posted on 2014-12-07 12:35  gate_s  阅读(1148)  评论(0编辑  收藏  举报