首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

新手

2012-01-31 
新手求助怎样取得每个月最后一个星期天是几号?语句:SQL server 2008[解决办法]SQL codeset datefirst 1dec

新手求助
怎样取得每个月最后一个星期天是几号?
语句: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) 

热点排行