读书人

新手

发布时间: 2012-01-31 21:28:41 作者: rapoo

新手求助
怎样取得每个月最后一个星期天是几号?
语句:SQL server 2008


[解决办法]

SQL code
set datefirst 1declare @year int=2011 ;with cte as (    select DATEADD(DAY,number,LTRIM(@year)+'0101') mon    from master..spt_values     where type='p' and number >=0     and DATEADD(DAY,number,LTRIM(@year)+'0101')<LTRIM(@year+1)+'0101'     and datepart(weekday,DATEADD(DAY,number,LTRIM(@year)+'0101'))=7)select * from cte twhere not exists(select * from cte where MONTH(mon)=MONTH(t.mon) and mon>t.mon)mon-----------------------2011-01-30 00:00:00.0002011-02-27 00:00:00.0002011-03-27 00:00:00.0002011-04-24 00:00:00.0002011-05-29 00:00:00.0002011-06-26 00:00:00.0002011-07-31 00:00:00.0002011-08-28 00:00:00.0002011-09-25 00:00:00.0002011-10-30 00:00:00.0002011-11-27 00:00:00.0002011-12-25 00:00:00.000(12 row(s) affected) 

读书人网 >SQL Server

热点推荐