gpt4 book ai didi

datetime - 根据开始日期和日期查询设置我的 To/From 日期

转载 作者:行者123 更新时间:2023-12-04 06:05:07 24 4
gpt4 key购买 nike

首先,感谢大家昨天的帮助。

我已经设法让我的报告工作(到目前为止),所以现在我需要在我的标题中显示日期时间段(从:到:)。这是查询:

Declare @startdate  datetime
Declare @enddate datetime
Declare @BeginningDate datetime

set @BeginningDate = '12-01-2011'

IF Month(@BeginningDate) < (Month(GETDATE())-1)--(YTD)
BEGIN
set @startdate = DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
set @enddate = dateadd(day, datediff(day, 0, getdate()), 0)
END

IF Month(@BeginningDate) = (Month(GETDATE())-1)--(MTD)
BEGIN
set @startdate = dateadd (mm,-1,
DATEADD(dd,-(DAY(DATEADD(mm,1,convert(varchar(10),getdate(),111)))-1),
DATEADD(mm,0,convert(varchar(10),getdate(),111))))--BEGINNING OF PRIOR MONTH
set @enddate =DATEADD(dd,-1,
DATEADD(mm,
DATEDIFF(m,0,convert(varchar(10),getdate(),111)),0))--END OF PRIOR MONTH
END


IF Month(@BeginningDate) = (Month(GETDATE()))--(Current Period)
BEGIN
set @startdate = dateadd(month, datediff(month, 0,
dateadd(day, datediff(day, 1, getdate()), 0)), 0)--BEGINNING OF CURRENT MONTH
set @enddate = dateadd(day,
datediff(day, 0, getdate()), 0)--THROUGH CURRENT MONTH (TODAY)
END

如果用户输入日期 11/27/201112-03-2011作为搜索参数,我的标题应该显示 FROM: 11/01/2011 TO:11/30/2011 (MTD) .例如,如果他们选择 12/01/201112/08/2011那么我只需要那些日期(当前期间)。如果用户输入 10/01/2011-12/8/2011那么该期间将来自 01/01/2011 to 12/8/2011 (YTD) .

我将如何在 SSRS 中将其设置为表达式?我是否需要根据这些输入创建数据集,然后调用参数?我已经使用一个日期时间框架完成了这项工作,但从来没有像这样存在多个时间框架问题的情况。

提前致谢!

更新:这就是我试图在我的 SSRS 表达式中完成的任务:
="FROM:"&iif(Month(Parameters!BeginningDate.Value < (Month(today()-1)) THEN (DateAdd("d",-DatePart(DateInterval.DayOfYear,Today(),0,0)+1,Today())).tostring("dd/MM/yyyy")+"       "+"TO:today()"

如果用户选择的开始日期大于上个月(例如 10/1),则返回的日期应为 1/1/201112/16/2011
我希望这能澄清事情。

谢谢!

最佳答案

我之前发布过这个,你需要使用一个可以调用日期函数的数据集。看看你是否可以关注这个:SQL Server: calculating date ranges它创造奇迹。具体看For Reporting Services Folks的部分.

对您来说,这将非常简单,如果他们选择 11/27/2011 并且您想返回 11/1/2011,您只需调用数据集,该数据集又会调用函数 month_start。它在该线程中有详细记录并被广泛使用。

它的要点是您需要在 RS 中使用此功能:

CREATE FUNCTION [dbo].[udfCommonDates] (@date datetime)
RETURNS @t table (week_start datetime,
week_end datetime,
lastweek_start datetime,
lastweek_end datetime,
month_start datetime,
month_end datetime,
lastmonth_start datetime,
lastmonth_end datetime,
yesterday_start datetime,
yesterday_end datetime,
today_start datetime,
today_end datetime,
thisweek_monday_start datetime,
thisweek_monday_end datetime,
year_start datetime,
year_end datetime,
tomorrow_noon datetime,
today_noon datetime,
date_only datetime)
BEGIN
INSERT @t
SELECT
dbo.get_week_start ( @date ) AS week_start,
dbo.get_week_end ( @date ) AS week_end,
dbo.get_week_start ( DATEADD(d, -7, @date ) ) AS lastweek_start,
dbo.get_week_end ( DATEADD(d, -7, @date ) ) AS lastweek_end,
dbo.get_month_start( @date ) AS month_start,
dbo.get_month_end ( @date ) AS month_end,
dbo.get_month_start ( DATEADD(m,-1, @date) ) AS lastmonth_start,
dbo.get_month_end ( DATEADD(m,-1,@date) ) AS lastmonth_end,
dbo.get_yesterday_start ( @date ) AS yesterday_start,
dbo.get_yesterday_end ( @date ) AS yesterday_end,
dbo.get_today_start (@date) AS today_start,
dbo.get_today_end ( @date ) AS today_end,
dbo.get_weekday_start(1,@date) AS thisweek_monday_start,
dbo.get_weekday_end(1,@date) AS thisweek_monday_end,
dbo.get_year_start(@date) AS year_start,
dbo.get_year_end(@date) AS year_end,
dbo.get_tomorrow_noon(@date) AS TomorrowNoon,
dbo.get_today_noon(@date) AS TodayNoon,
dbo.get_date_only(@date) AS DateOnly
RETURN
END

然后你需要每个函数的标量值函数:
   CREATE FUNCTION [dbo].[get_date_only] (@date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN dateadd(day, DateDiff(day, 0, GetDate()), 0)
END
GO

CREATE FUNCTION [dbo].[get_month_end] (@date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN dateadd(ms, -3, dateadd (m,datediff(m,0,
dateadd(m,1,@date)),0))
END
GO

CREATE FUNCTION [dbo].[get_month_start] (@date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN dateadd(m,datediff(m,0, @date),0)
END
GO

CREATE FUNCTION [dbo].[get_today_end] (@today datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
return dateadd(ms, -3, datediff(d,0,dateadd(d,1,@today)))
END
GO

CREATE FUNCTION [dbo].[get_today_noon](@date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
RETURN DATEADD(hh, 12, DATEADD(d,DATEDIFF(d,0, @date),0))
END
GO

CREATE FUNCTION [dbo].[get_today_start] (@today datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(day, 0, datediff(d,0,@today))
END
GO

CREATE FUNCTION [dbo].[get_tomorrow_noon](@date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
RETURN DATEADD(hh, 12, DATEADD(d,DATEDIFF(d,-1, @date),0))
END
GO

CREATE FUNCTION [dbo].[get_week_end] (@date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(yyyy, datepart(yyyy,
dateadd(weekday,7-datepart(weekday, @date),@date))-1900, 0)
+ dateadd(ms, -3,
dateadd(dy, datepart(dy,
dateadd(weekday,7-datepart(weekday, @date),@date)),0) )
END
GO

CREATE FUNCTION [dbo].[get_week_start] (@date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(yyyy, datepart(yyyy,
dateadd(weekday,1-datepart(weekday, @date),@date))-1900, 0)
+ dateadd(dy, datepart(dy,
dateadd(weekday,1-datepart(weekday, @date),@date))-1,0)
END
GO

CREATE FUNCTION [dbo].[get_weekday_end] (@weekday tinyint,
@date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(yyyy, datepart(yyyy,
dateadd(weekday,@weekday-
datepart(weekday, @date),@date))-1900, 0)
+ dateadd(ms, -3,
dateadd(dy, datepart(dy,
dateadd(weekday,@weekday-datepart(weekday, @date),
@date)),0) )
END
GO

CREATE FUNCTION [dbo].[get_weekday_start] (@weekday tinyint,
@date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(yyyy, datepart(yyyy,
dateadd(weekday,@weekday-
datepart(weekday, @date),@date))-1900, 0)
+ dateadd(dy, datepart(dy,
dateadd(weekday,@weekday-datepart(weekday, @date),
@date))-1,0)
END
GO

CREATE FUNCTION [dbo].[get_year_end] (@date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
RETURN DATEADD(year, DATEDIFF(year, 0, GetDate())+1, 0)-1
END
GO

CREATE FUNCTION [dbo].[get_year_start] (@date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
RETURN DATEADD(year,DATEDIFF(year,0, @date),0)
END
GO

CREATE FUNCTION [dbo].[get_yesterday_end] (@today datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(ms, -3, datediff(d,0,@today))
END
GO

CREATE FUNCTION [dbo].[get_yesterday_start] (@today datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
RETURN dateadd(day, -1, datediff(d,0,@today))
END
GO

现在为报告服务完成了艰苦的工作,记住数据集可以是存储过程或直接表。因此,您只需创建一个存储过程:
CREATE PROCEDURE [dbo].[uspCommonDates] AS
begin
set datefirst 1
declare @date datetime
set @date = getdate()
select * from dbo.udfCommonDates(@date)
end

并将此存储过程用作 dataset .所以现在您可以访问所有这些不错的日期函数。因此,您现在可以转到参数部分并像这样调用这些函数:

enter image description here

关于datetime - 根据开始日期和日期查询设置我的 To/From 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8448743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com