I have a report in SSRS. In my main report I am linking to another report using a link action.
I need to pass in two variables to linked report. One for startdate and one for enddate. I need to pass in start date as first day of current month 12.00am and enddate as yesterday 1259pm
Trying to do this with an expression. Any help appreciated.
I can do this with sql as below, but need to convert this to expression used in SSRS.
Set #startdate = DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
Set #enddate = DATEADD(ms,-3, DATEADD(day, DATEDIFF(day,0,GETDATE()),0))
try using expression
First Day of current month
=DateSerial(Year(Now()), Month(Now()), "1")
Previous Day (Yesterday)
=DateSerial(Year(Now()), Month(Now()), Day(Now())).AddDays(-1) & " 23:59:59"
Related
I have a report on daily base date and I get a table where it display data of today when I run for today date.
Then I have a line chart which shows the graph of that date and the count of hit. Here, I need the graph to be shown for the whole month when I run the report not for only single date.
Please let me know the logic .
Thank you
Create a new Dataset and define variables containing the range of data you need for whole month graph. Then select data using the variables.
Declaring variables:
declare #startOfMonth datetime2 = (SELECT DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
declare #endOfMonth datetime2 = getdate()
Next, use this variables in your query:
select
/*
all data you need
*/
where date_of_data >= #startOfMonth and date_of_data <= #endOfMonth
Then use this Dataset in your new monthly graph.
I have an SSRS report that I would like to set the default end date to be 6 months prior to today's date and the start date to be 2 years prior to the end date. If I run it in September, End date would be March, next month End date should automatically update to April. How can I configure this in SSRS?
Try this:
Start Date:=DateAdd(m, -30, GetDate())
End Date:=DateAdd(m, -6, GetDate())
Here are great references:
SQL Server DATEADD() Function
SQL Date Functions
The below expression will work as per your need, you need to put these in the parameters default value section and opt for "specify values" -
StartDate =DateAdd(DateInterval.Month,-30,Now())
EndDate =DateAdd(DateInterval.Month,-6,NOW())
Also choose the parameters as Date/Time.
Below Snapshot to show the difference I chose Start Date as Date/Time and End Date as Text
Create the parameters, and set the Default values as below:
Start Date:=DateAdd(DateInterval.Month, -30, Today()))
End Date:=DateAdd(DateInterval.Month, -6, Today()))
Use these parameter values in the data set queries to filter out the results.
We've been using MS Access, with the following syntax for MTD Data that works for us:
Between DateSerial(Year(Date()),Month(Date()),1)
And DateSerial(Year(Date()),Month(Date())+1,0)
We need to transition the above logic to SQL/SSRS for automatic emailed reports, but I cannot get this DateSerial logic to work with SQL.
In the Filter field of the SQL query, I can successfully use BETWEEN '8/1/2014' AND '8/31/2014' for MTD data, but would like to have a DateSerial logic applied so that reports don't need to be created for every month, quarter, year, etc.
When trying to use the DateSerial function, we get the error "Invalid or missing Expression". I've seen a few topics on this that Parameters are required, but really believe that this is a simple syntax issue for the filter field, since actual dates work with the BETWEEN command.
There are several different ways to get this. Here is just one way.
Get todays date. In this case 8/27/2014
Declare #Today date = cast(getdate() as date)
Get the first of the month, 26 days in the past
Declare #StartDate date = dateadd(d, -1 * (day(#Today) - 1), #Today)
select #Today, #StartDate
You can use the function CONVERT:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Or the function DATEFROMPARTS if you are using SQL Server 2012:
http://msdn.microsoft.com/en-us/library/hh213228.aspx
Or DATEADD:
select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0); -- first day of current month
select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()), -1) -- last day of current month
This last one I took from: https://stackoverflow.com/a/11746042/1274092
See mine at:
http://sqlfiddle.com/#!3/d41d8/38333
This has been resolved. The ODBC driver does not apparently play well with SSRS. The DateSerial command would not work within the query itself. The workaround was to add the filter to the Dataset. This syntax is what works, but again only in the Dataset filter: [expression] Between [first value box] =DateSerial(Year(Now()),1,1) [second value box] =DateSerial(Year(Now()),12,31)
This gives us the YTD reporting data that we require.
I am having a bit of an issue with my SSRS report I am trying to run. I am trying to have a report pull from the beginning of the day (to be ran daily) and the execution time.
I have:
(Rest of the SQL here)WHERE fa.ReceivedDate between #prmStartDate and %executionTime
In #prmStartDate currently I have =DateValue(today) which is creating an error when running.
Would this not work?
where ReceivedDate >= cast(getdate() as date)
and ReceivedDate < getdate()
If it will always use the current date, then you could use the GETDATE function in SQL server and you just need a way to strip off the time portion on the first date. A method I like is suggested here.
Your code would look like this:
(Rest of the SQL here)WHERE fa.ReceivedDate between DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) and GETDATE()
If you want to allow the user to choose a different day's data, just add a parameter instead of the GETDATE function:
(Rest of the SQL here)WHERE fa.ReceivedDate between #DateParam and DATEADD(dd, 1, #DateParam)
Ok I am trying to write a query that says get the current date and make it the start date. Then I want to go a month back from that current date for the EndDate. Is it possible to do this? Like if it was 9-15-2010 as the start date can I go a month back from that to 8-15-2010 or is this no possible....what would you do for like 9-20-2010 as the start date since every month has a different amount of days in it? Otherwise if this is not possible how else could I do this? The report will always be run on the 25th of the month so any ideas? I need to go from the 25th back a month....I can get some duplicate records between months if needed but less is obviously better
Right now I am using this:
DECLARE #StartDate DATETIME,
#EndDate DATETIME;
SET #StartDate = DATEADD(m,-1,GETDATE());
SET #EndDate = DATEADD(m, 1, #StartDate);
Does this work?
Also, how would I then say my AuditInsertTimestamp is between #Start adn #EndDate?
Currently I have this:
AND cvn.[AuditInsertTimestamp] BETWEEN #StartDate AND #EndDate ;
This is still giving me dates like 7-26-2010 though....
Thanks!
That should work. Did you try it?
If it doesn't work (and there are only 12 test cases to check if you don't trust the documentation) then you can re-build the date from the date parts.
Here's the problem. It should be like this:
cvn.[Subject] = 'Field Changed (Plate Type)'
AND (
cvn.[Note] LIKE 'Old Type: IRP%New Type: BASE PLATE%'
OR cvn.[Note] LIKE 'Old Type: Base Plate%New Type: IRP%'
)
AND cvn.AuditInsertTimestamp BETWEEN GETDATE() AND DATEADD(MONTH, -1, GETDATE())
AND takes precidence over OR, so you were picking up anything with Old Type:IRP or in the correct date range (with Old Type: Base Plate)
Based on your comment:
Well this is being used to select
records. So if I run it on the 25th I
need 30 days back then my field
AuditInsertTimestamp needs to be
between these 2 dates.
I think you need to do something like this:
SELECT * FROM Table
WHERE AuditInsertTimestamp BETWEEN GETDATE() AND DATEADD(MONTH, -1, GETDATE())