比如下面字符串大小6000字符左右,我查询时传入查询条件时间 如:
2012-01-13 到  2012-10-13  在数据在范围内
2012-02-13 到  2012-12-13  在数据在范围内
2011-01-11 到  2011-10-13  不在范围内
 
d='2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|'请问有什么好方法、

解决方案 »

  1.   

    如果是你这种从小到大排列方式,直接取@min最小值2012-01-16,最大值@max2012-12-11(这个应很容易取的),如果最小值最大值不是按顺序排列,用两个函数取:也拆分成表,分别返回最小值,最大值对于你传入的日期这样判断,三种情况:a.开始日期在@min前,则结束日期大于开始日期才在范围内
    b.开始日期在@min和@max之间,则也在范围内
    c.其他就是不在范围内的了
      

  2.   


    declare @d varchar(max),
            @n int
    set     @n=10
    set     @d='2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|'
    select substring(@d,1,@n) 
    /*
    2012-01-16
    */ 
    select substring(@d,@n+2,10)
    /*
    2012-04-12
    */select substring(@d,2*@n+3,10)/*
    2012-07-12
    */select substring(@d,3*@n+4,10)
    /*
    2012-07-12
    */
    --是有规律的,找出来写个while循环得出的结果和时间区间比较declare @d varchar(1000),
            @n int,
            @date datetime
    set     @n='10'
    set     @d='2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|'
    set     @date=''
    select @date=substring(@d,3*@n+4,10) if  @date>'2012-01-13' and @date<'2012-10-13'
    print 'yes'/*
    yes
    */
      

  3.   


    declare @d varchar(max),
            @n int
    set     @n=10
    set     @d='2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|'
    select substring(@d,1,@n) 
    /*
    2012-01-16
    */ 
    select substring(@d,@n+2,10)
    /*
    2012-04-12
    */select substring(@d,2*@n+3,10)/*
    2012-07-12
    */select substring(@d,3*@n+4,10)
    /*
    2012-07-12
    */
    --是有规律的,找出来写个while循环得出的结果和时间区间比较declare @d varchar(1000),
            @n int,
            @date datetime
    set     @n='10'
    set     @d='2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|'
    set     @date=''
    select @date=substring(@d,3*@n+4,10) if  @date>'2012-01-13' and @date<'2012-10-13'
    print 'yes'/*
    yes
    */
      

  4.   

    但数据一般都很多 while循环太慢了。
      

  5.   

    这里有个问题就是,如果选择min 与max 日期 在空档中间,如何判断呢如下。
    2011-01-18 到 2011-02-19 不在范围内 
     
    d='2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|'
      

  6.   


    2012-01-13 到 2012-10-13 在数据在范围内
    2012-02-13 到 2012-12-13 在数据在范围内
    2011-01-11 到 2011-10-13 不在范围内create table #test(
    dt datetime
    )
    declare @d varchar(8000)
    set @d='2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|'select @d=replace(@d,'|',''''+' union all  select '+'''')
    select @d='insert #test select '+''''+left(@d,len(@d)-20)
    --print @d
    exec(@d)select * from #test/*
    dt
    -----------------------------------
    2012-01-16 00:00:00.000
    2012-04-12 00:00:00.000
    2012-07-12 00:00:00.000
    2012-08-12 00:00:00.000
    2012-10-12 00:00:00.000
    2012-12-11 00:00:00.000
    */我把字符串给你分解了,不知道你那个在与不在怎么判断
      

  7.   


    --帮你写个完整的吧,如果逻辑有问题,自已稍改下
    --拆分函数
    if object_id('fn_test') is not null 
    drop function dbo.fn_test
    go
    create function dbo.fn_test(@str varchar(max),@split varchar(10),@min int)
    returns nvarchar(50)
    as
    begin    
    declare @xml xml
    declare @table table(col nvarchar(50))   
    select @xml = convert(xml,'<items><item col="' + replace(@str, @split, '"/><item col="') + '"/></items>')    
    insert into @table select xmlroot.item.value('@col[1]', 'nvarchar(50)') from @xml.nodes('//items/item') as xmlroot(item)    
    return (select case when @min=1 then min(col) else max(col) end from @table where isnull(col,'')<>'');
    end
    go
    --存储过程
    if object_id('p_test') is not null drop procedure p_test
    go
    create proc p_test(
    @str nvarchar(max),@start varchar(10),@end varchar(10)
    )
    as
    select @str='2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|';
    select case when @start<=dbo.fn_test(@str,'|',1) and @end>dbo.fn_test(@str,'|',1) then '在数据在范围内'
    when @start between dbo.fn_test(@str,'|',1) and dbo.fn_test(@str,'|',0) then '在数据在范围内'
    else '不在范围内' end;
    go
    --测试
    exec p_test '2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|','2012-01-13','2012-10-13';
    exec p_test '2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|','2012-02-13','2012-12-13';
    exec p_test '2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|','2011-01-11','2011-10-13';
    exec p_test '2012-01-16|2012-04-12|2012-07-12|2012-08-12|2012-10-12|2012-12-11|','2011-01-18','2011-02-19';
    --结果
    /*
    --------------
    在数据在范围内(1 行受影响)
    --------------
    在数据在范围内(1 行受影响)
    --------------
    不在范围内(1 行受影响)
    --------------
    不在范围内(1 行受影响)
    */