想做成存储过程,要求输入了一个日期字符串如20090604日,返回id1,id2,id3,字段where1是1,2,3,4,5

解决方案 »

  1.   

    id, type,     where1,          where2
    N1   星期       1,2,3,4,5//星期一,
    B2   每月       1,2,3,4,5//每月1号
    C3   隔月       1,2,3,4,5         1,2,3,4,5//1月1号,1月2号,同理
    D4   单数周     1,2,3,4,5 //星期一
    E5   双月周     1,2,3,4,5  //星期一
      

  2.   

    id, type,     where1,          where2
    N1   星期       1,2,3,4,5//星期一,
    B2   每月       1,2,3,4,5//每月1号
    C3   隔月       1,2,3,4,5         1,2,3,4,5//1月1号,1月2号,同理
    D4   单数周     1,2,3,4,5 //星期一
    E5   双月周     1,2,3,4,5  //星期一
      

  3.   

    比如输入一个20090604日的时候,判断这天有哪几个id,20090604日,是周四,上表数据应该有n1,因为WHERE1中有周四,同时也满足b2因为是每月的4号,不满足c3因为where1有中没有6月,同时满足D4,因为是单数周,6月份的第一周有周4,同理E5是双月周,6月是双月,也有周4,所以返回的应该是N1,B2,D4,E5
      

  4.   


    --简体繁体注意改下
    create table T(id char(02),type nvarchar(10),where1 nvarchar(30), where2 nvarchar(30))
    insert into T
    select 'N1','星期','1,2,3,4,5',''
    union all select 'B2','每月','1,2,3,4,5',''
    union all select 'C3','隔月','1,2,3,4,5','1,2,3,4,5'
    union all select 'D4','單數周','1,2,3,4,5',''
    union all select 'E5','雙月周','1,2,3,4,5',''GOdeclare @input datetime
    set @input ='2009-06-04'
    declare @w1  int, @w2 int,@w3_1 int, @w3_2 int, @w4 int,@w5 int
    --星期, 默認星期天是1,星期一是2...類推
    select @w1=datepart(weekday, @input) -1
    --每月
    select @w2=day(@input)
    --隔月
    select @w3_1=month(@input), @w3_2=day(@input)
    --單數周
    select @w4= ( datepart(week,@input) - datepart(week, convert(datetime, convert(char(07),@input,120)+'-01'))+1)%2
    --雙月周
    select @w5=month(@input)%2 select * from T
    where (type=N'星期' and  charindex(rtrim(@w1) ,where1)>0)
       or    (type=N'每月' and charindex(','+rtrim(@w2)+',' , ','+rtrim(where1)+',')>0)
       or    (type=N'隔月' and charindex(','+rtrim(@w3_1)+',' , ','+rtrim(where1)+',')>0  and  charindex(','+rtrim(@w3_2)+',' , ','+rtrim(where2)+',')>0 )
       or    (type=N'單數周' and  @w4=1  and   charindex(rtrim(@w1) ,where1)>0 )
       or    (type=N'雙月周' and @w5=0  and   charindex(rtrim(@w1) ,where1)>0 )GO
    drop table T