概述:我想通过输入参数startdate和enddate显示每周结果。我得到的结果非常好。但问题是,当我想从2015年8月28日开始约会结束日期04/09/2015上午从同月(8月)获得28,29,30,31,01,02,03,04。预计结果应为8月28日,29日,30日,31日和9月1日,02日,03日,04日。
帮我解决这个问题。以下是我的代码
local all postgres peer
local all all trust
local all all 127.0.0.1/32 trust
host all all ::1/128 md5
编辑:我试过if if条件,如果(monthdifference等于0) else(monthdifference大于0)。但没有得到预期的结果。
答案 0 :(得分:1)
试试这个
Declare
@StartDate datetime='2015/08/28',
@EndDate datetime='2015/09/04'
;WITH sample AS (
SELECT CAST(@StartDate AS DATETIME) AS dt
UNION ALL
SELECT DATEADD(dd, 1, dt)
FROM sample s
WHERE DATEADD(dd, 1, dt) <= CAST(@EndDate AS DATETIME))
SELECT *
FROM sample
输出是:
2015-08-28 00:00:00.000
2015-08-29 00:00:00.000
2015-08-30 00:00:00.000
2015-08-31 00:00:00.000
2015-09-01 00:00:00.000
2015-09-02 00:00:00.000
2015-09-03 00:00:00.000
2015-09-04 00:00:00.000
答案 1 :(得分:0)
确保在动态查询中声明了startdate和enddate
答案 2 :(得分:0)
主要想法是:
- 您不需要使用STUFF。只需从DATEADD(DAY,1,@ startdate)
中选择日期即可- 如果DATENAME(MONTH,@ startdate),你应该得到日期twise!= DATENAME(MONTH,@ enddate)。第一次从开始到月末。第二次从第二个月开始到结束。
醇>
我的(已选中)脚本。
ALTER PROCEDURE [dbo].[usp_Get_TimesheetDetails]
@UserID int, @startdate datetime, @enddate datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare @columns varchar(max);
DECLARE @sqlText nvarchar(1000);
Declare @temptable table (num date );
WITH gen AS (
SELECT DATEADD(DAY,1,@startdate) AS num
UNION ALL
SELECT DATEADD(DAY,1,num) FROM gen
WHERE DATEADD(DAY,1,num) <= @enddate
and DATEADD(DAY,1,num) < dateadd(month,datediff(month,0,@enddate),0)
)
insert into @temptable SELECT num FROM gen
option (maxrecursion 10000)
set @columns=
(SELECT distinct
STUFF((SELECT ',' + CAST( DATEPART(DAY, num) as varchar(100)) [text()]
FROM @temptable
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,1,'') List_Output
FROM @temptable t)
SET @sqlText = N'SELECT [' + REPLACE(@columns,',','],[') + ']' + ' FROM dbo.timesheet where month ='''+ DATENAME(MONTH, @startdate)+''' and [Task ID] in(select TaskID from ManageTasks where TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')';
print @sqlText;
IF DATENAME(MONTH, @startdate)!=DATENAME(MONTH, @enddate)
BEGIN
delete from @temptable;
WITH gen AS (
SELECT dateadd(month,datediff(month,0,@enddate),0) AS num
UNION ALL
SELECT DATEADD(DAY,1,num) FROM gen
WHERE DATEADD(DAY,1,num) <= @enddate
)
insert into @temptable SELECT num FROM gen
option (maxrecursion 10000)
set @columns=
(SELECT distinct
STUFF((SELECT ',' + CAST( DATEPART(DAY, num) as varchar(100)) [text()]
FROM @temptable
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,1,'') List_Output
FROM @temptable t)
SET @sqlText = N'SELECT [' + REPLACE(@columns,',','],[') + ']' + ' FROM dbo.timesheet where month ='''+ DATENAME(MONTH, @enddate)+''' and [Task ID] in(select TaskID from ManageTasks where TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')';
print @sqlText
end
end
答案 3 :(得分:0)
您可能会发现pivot / unpivot更强大。当然你的桌子设计并不理想。 (为了演示目的,我减少了列数。)
create table dbo.timesheet (
[month] varchar(12) not null,
[1] int null, [2] int null, [3] int null,
[28] int null, [29] int null, [30] int null, [31] int null
);
declare @startDate date = '20160628';
declare @endDate date = '20160703';
insert into dbo.timesheet ([month], [1], [2], [3], [28], [29], [30], [31])
values ('June', 1, 2, 3, 4, 5, 6, null), ('July', 8, 9, 10, 11, 12, 13, 14);
with hrs as (
select
hrs,
dy,
dateadd(
month,
case [month]
when 'January' then 1 when 'February' then 2 when 'March' then 3
when 'April' then 4 when 'May' then 5 when 'June' then 6
when 'July' then 7 when 'August' then 8 when 'September' then 9
when 'October' then 10 when 'November' then 11 when 'December' then 12
end,
dateadd(year, year(getdate()) - 2000, dateadd(day, dy - 1, '19991201'))
) as dt
from
(select [month], [1], [2], [3], [28], [29], [30], [31] from dbo.timesheet) t
unpivot (hrs for dy in ([1], [2], [3], [28], [29], [30], [31])) as upvt
)
select datename(month, dt), [1], [2], [3], [28], [29], [30], [31]
from hrs pivot (min(hrs) for dy in ([1], [2], [3], [28], [29], [30], [31])) as pvt
where dt between @startDate and @endDate;