日期:2014-05-19  浏览次数:20449 次

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

------解决方案--------------------
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

------解决方案--------------------
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
------解决方案--------------------
declare @s varchar(20)
set @s= '2007-7 '
set @s=@s+ '-1 '
select dateadd(dd,9-DATEPART(dw,@s),@s)
------解决方案--------------------
set datefirst 1
declare @d varchar(20)
set @d= '200706 '

set @d=@d+ '01 '
select dateadd(day,15-DATEPART(weekday,@d),@d)
------解决方案--------------------
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