我使用此查询获取一个月内的全天
declare @date datetime
set @date = '20140201';
with DaysInMonth as (
select @date as Date
union all
select dateadd(dd,1,Date) from DaysInMonth where month(date) = month(@Date)
)
select * from DaysInMonth where month(date) = month(@Date)
并在列表中获取一整天
我如何得到这个结果,并把这几天放在相应的一周 例如本月:
Sun1 - Mon1 - TUE1 - WED1 - THU1 - FRI1 - SAT1 - Sun2 - Mon2 - TUE2 - WED2 - THU2 - FRI2 - SAT2
- - - - - - 1 2 3 4 5 6 7 8
pd:与周相对应的数字1和2
答案 0 :(得分:0)
没关系????
declare @date datetime
set @date = '20140301';
with DaysInMonth as (
select @date as Date,DATENAME(DD,@date) as [DAY],DATENAME(WEEKDAY,@date) as [DAYNAME]
union all
select dateadd(dd,1,Date),DATENAME(DD,Date+1) as [DAY],DATENAME(WEEKDAY,Date+1) as [DAYNAME] from DaysInMonth where month(date) = month(@Date)
)
select [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]
--Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday
from
(
SELECT [DAY],[DAYNAME] FROM DaysInMonth where month(date) = month(@Date)
) D
PIVOT (MIN([DAYNAME]) FOR [DAY] IN ([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]) ) AS PVT
我对你的确切要求的另一个答案
declare @date datetime
declare @lastWeek int
set @date = '20140301';
set @lastWeek = DATENAME(WW,@date)-1
;with DaysInMonth as (
select @date as Date,
DATENAME(DD,@date) as [DAY],
DATENAME(WEEKDAY,@date)+cast(cast(DATENAME(WW,@date) as int)-@lastWeek as varchar(2)) as [DAYNAME]
union all
select dateadd(dd,1,Date),DATENAME(DD,Date+1) as [DAY],
DATENAME(WEEKDAY,Date+1)+cast(cast(DATENAME(WW,Date+1) as int)-@lastWeek as varchar(2)) as [DAYNAME]
from DaysInMonth
where month(date) = month(@Date)
)
select
Sunday1,Monday1,Tuesday1,Wednesday1,Thursday1,Friday1,Saturday1,
Sunday2,Monday2,Tuesday2,Wednesday2,Thursday2,Friday2,Saturday2,
Sunday3,Monday3,Tuesday3,Wednesday3,Thursday3,Friday3,Saturday3,
Sunday4,Monday4,Tuesday4,Wednesday4,Thursday4,Friday4,Saturday4,
Sunday5,Monday5,Tuesday5,Wednesday5,Thursday5,Friday5,Saturday5,
Sunday6,Monday6,Tuesday6,Wednesday6,Thursday6,Friday6,Saturday6
from
(
SELECT [DAY],[DAYNAME] FROM DaysInMonth where month(date) = month(@Date)
) D
PIVOT (max([DAY]) FOR [DAYNAME] IN
(Sunday1,Monday1,Tuesday1,Wednesday1,Thursday1,Friday1,Saturday1,
Sunday2,Monday2,Tuesday2,Wednesday2,Thursday2,Friday2,Saturday2,
Sunday3,Monday3,Tuesday3,Wednesday3,Thursday3,Friday3,Saturday3,
Sunday4,Monday4,Tuesday4,Wednesday4,Thursday4,Friday4,Saturday4,
Sunday5,Monday5,Tuesday5,Wednesday5,Thursday5,Friday5,Saturday5,
Sunday6,Monday6,Tuesday6,Wednesday6,Thursday6,Friday6,Saturday6
)
) AS PVT
答案 1 :(得分:0)
试试这段代码,
declare @date datetime
set @date = '20140901';
with DaysInMonth as (
select @date as Date,DATENAME(DD,@date) as [DAY],DATENAME(WEEKDAY,@date) as [DAYNAME]
union all
select dateadd(dd,1,Date),DATENAME(DD,Date+1) as [DAY],DATENAME(WEEKDAY,Date+1) as [DAYNAME] from DaysInMonth where month(date) = month(@Date)
)
select [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],ISNULL([29],'-')[29],ISNULL([30],'-')[30],ISNULL([31],'-')[31]
from
(
SELECT [DAY],[DAYNAME] FROM DaysInMonth where month(date) = month(@Date)
) D
PIVOT (MIN([DAYNAME]) FOR [DAY] IN ([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]) ) AS PVT