2

首先,感謝大家的幫助昨天。設置我要開始日期基於/的開始日期和時間日期查詢

我設法讓我的報告工作(到目前爲止),所以現在我需要顯示日期時間(FROM:在我的頭TO :)。下面是該查詢:

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其設置爲一個表達式?我是否需要根據這些輸入創建一個數據集,然後調用一個參數?我已經用一個日期時間框架完成了這個工作,但從來沒有像這樣的時間框架問題。

在此先感謝!

UPDATE:這是什麼,我想在我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

我希望這個澄清的事情。

謝謝!

+0

@Ken白色。你如何通過這種方式獲得「高精簡版」的日期?你是否像縮放代碼一樣縮進?仍然試圖在這裏學習交易的所有技巧,以最大限度地減少火焰:D謝謝! –

+0

反斜槓(後面的撇號,與美國鍵盤上'〜Esc'下面的〜相同的鍵)圍繞它們,將它們視爲內聯代碼。 –

+0

謝謝!當我在前面的問題中注意到這個時,我忘了問這個問題。 –

回答

3

我之前張貼了這個,你需要使用的數據集,你可以調用最新的功能。看看你是否可以按照這個:SQL Server: calculating date ranges它的奇蹟。具體看看For Reporting Services Folks的部分。

對你來說將是非常簡單的,如果他們選擇2011/11/27並要返回11/1/2011你只需要調用它輪流調用函數MONTH_START數據集。它在該線程中都有很好的記錄並廣泛使用。

它的JIST是你需要在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

相關問題