日期:2014-05-18 浏览次数:20637 次
declare @month as varchar(7) set @month = '2007-12' select 日,一,二,三,四,五,六 from ( select week , max(case weekday when 1 then datename(day,dt) else '' end ) '日', max(case weekday when 2 then datename(day,dt) else '' end ) '一', max(case weekday when 3 then datename(day,dt) else '' end ) '二', max(case weekday when 4 then datename(day,dt) else '' end ) '三', max(case weekday when 5 then datename(day,dt) else '' end ) '四', max(case weekday when 6 then datename(day,dt) else '' end ) '五', max(case weekday when 7 then datename(day,dt) else '' end ) '六' from ( select week = datepart(week , m.dt) , weekday = datepart(weekday , m.dt) , dt from ( select dt = @month + '-' + right('00'+cast(t.id as varchar),2) from ( select 1 as id union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10 union select 11 union select 12 union select 13 union select 14 union select 15 union select 16 union select 17 union select 18 union select 19 union select 20 union select 21 union select 22 union select 23 union select 24 union select 25 union select 26 union select 27 union select 28 union select 29 union select 30 union select 31 ) t where isdate(@month + '-' + right('00'+cast(t.id as varchar),2)) = 1 and @month + '-' + right('00'+cast(t.id as varchar),2) <= dateadd(month , 1 , @month + '-01') ) m ) n group by week ) o /* 日 一 二 三 四 五 六 -- -- -- -- -- -- -- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 (所影响的行数为 6 行) */
------解决方案--------------------
set datefirst 1 select 星期,星期一,星期二,星期三,星期四,星期五,星期六,星期日 from ( select ltrim(datepart(ww,t_date))+'00' rowid,'日期' [星期], max(case when datepart(dw,t_date)=1 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期一], max(case when datepart(dw,t_date)=2 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期二], max(case when datepart(dw,t_date)=3 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期三], max(case when datepart(dw,t_date)=4 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期四], max(case when datepart(dw,t_date)=5 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期五], max(case when datepart(dw,t_date)=6 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期六], max(case when datepart(dw,t_date)=7 then ltrim(month(t_date))+'月