怎么抓某年月第二个周一是几号,在线结贴..

解决方案 »

  1.   

    declare @s varchar(20)
    set @s='2007-6'               --年月select top 1 @s+'-'+cast(d as varchar)
    from (
    select 1 as d
    union all
    select 2
    union all
    select 3
    union all
    select 4
    union all
    select 5
    union all
    select 6
    union all
    select 7
    union all
    select 8
    union all
    select 9
    union all
    select 10
    union all
    select 11
    union all
    select 12
    union all
    select 13
    ) as t
    where datepart(weekday,@s+'-'+cast(d as varchar))=2
    order by d desc
      

  2.   

    上面结果
    2007-6-11declare @s varchar(20)
    set @s='2007-7'               --年月select top 1 @s+'-'+cast(d as varchar)
    from (
    select 1 as d
    union all
    select 2
    union all
    select 3
    union all
    select 4
    union all
    select 5
    union all
    select 6
    union all
    select 7
    union all
    select 8
    union all
    select 9
    union all
    select 10
    union all
    select 11
    union all
    select 12
    union all
    select 13
    ) as t
    where datepart(weekday,@s+'-'+cast(d as varchar))=2
    order by d desc--结果
    2007-7-9
      

  3.   

    declare @s varchar(20)
    declare @t datetime
    set @s='2007-6'
    set @t=convert(datetime,@s+'-1')
    select DATEADD(wk, DATEDIFF(wk,0, dateadd(dd,6-datepart(day,@t),@t) ), 7) --结果:2007-6-11
      

  4.   

    declare @s varchar(20)
    set @s='2007-7' 
    set @s=@s+'-1'
    select dateadd(dd,9-DATEPART(dw,@s),@s)
      

  5.   

    declare @s varchar(20)
    declare @t datetime
    set @s='2007-2'
    set @t=convert(datetime,@s+'-1')
    select DATEADD(wk, DATEDIFF(wk,0, dateadd(dd,6-datepart(day,@t),@t) ), 7) 
    --结果:2007-2-12
      

  6.   

    set datefirst 1
    declare @d varchar(20)
    set @d='200706'set @d=@d+'01'
    select dateadd(day,15-DATEPART(weekday,@d),@d)
      

  7.   

    DECLARE @y nvarchar(5)
    declare @m nvarchar(2)
    set @y='2007'
    set @m='12'
    declare @a datetime
    set @a=@y+'-'+@m+'-1'
    select  dateadd(d,(8-(datepart(dw,@a)-1))%7+7,@a)--2007-12-10 00:00:00.000
      

  8.   

    DECLARE @y nvarchar(5)
    declare @m nvarchar(2)
    set @y='2007'
    set @m='1'
    declare @a datetime
    set @a=@y+'-'+@m+'-1'
    select  dateadd(d,(8-(datepart(dw,@a)-1))%7+7,@a)--2007-01-08 00:00:00.000
      

  9.   

    6月的第二个周一应该是4号吧我觉得
    declare @s varchar(20)
    set @s='2007-7' 
    set @s=@s+'-1'
    select dateadd(dd,9-DATEPART(dw,@s),@s)这个贴好.学习了。