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.
Related
In a SQL Server query, I am currently using the clause
WHERE
DAY(trade_date) = DAY(GETDATE()) - 1
AND MONTH(trade_date) = MONTH(GETDATE())
AND YEAR(trade_date) = YEAR(GETDATE())
to query my data from the previous day.
It is working fine right now but my question is if, for example, on 8/1/2021, SQL Server will try to get data from 8/0/2021 or if it will know to get data from 7/31/2021.
If this query won't work what could I use instead? Thanks!
I would recommend using proper date comparison logic - instead of breaking it down to day, month and year. Also, it is recommended to use proper date arithmetic functions like DATEADD instead of just - 1 on your date values (never sure what that -1 stands for: minus one day? Week? Month? Hour?).
And lastly - I would also recommend using SYSDATETIME() instead of GETDATE() since the latter always returns a DATETIME datatype - which should be on its way out, and you should use DATE (if you don't need to time portion), or DATETIME2(n) (if you do need the time portion) since those are more efficient and have fewer limitations compared to DATETIME.
If your trade_date is a DATE column (as it probably should be), just use:
WHERE
trade_date = DATEADD(DAY, -1, SYSDATETIME())
and if it's not a DATE - just cast it to a date as needed:
WHERE
CAST(trade_date AS DATE) = DATEADD(DAY, -1, CAST(SYSDATETIME() AS DATE))
I am tring to add a year to a date if it is not before today. So in the statement below I would like to display 4/20/2018
declare #StartDate datetime
set #StartDate='4/20/2015'
select case when dateadd(year,1,#StartDate)> GETDATE() then
dateadd(year,1,#StartDate) else dATEADD(year,1,(datepart(year,GETDATE()))) end
I've changed the else clause so it works out the difference between the start date and today's date +1 to alter make '4/20/2015' become '4/20/2018'
declare #StartDate date
set #StartDate='4/20/2015'
select
case
when dateadd(year,1,#StartDate)> GETDATE() then dateadd(year,1,#StartDate)
else DATEADD(year,(datepart(year,GETDATE())+1-datepart(year,#StartDate)),#StartDate)
end
Also if you are not using the time part of a datetime data type it's best practice to use date instead as it only requires 3 bytes of storage rather than 8 for datetime.
More on date here: https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-transact-sql
And a cheatsheet of sql data types here: https://sqlserverrider.files.wordpress.com/2013/04/pic116.png
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"
I need to compare current day with some value. Like from front end I am passing 5, then in t-sql I want to match if the current day of the month is 5.
You can retrieve parts of the date with DATEPART.
DATEPART ( datepart , date )
DATEPART( MM, GETDATE())
Should return an INT value of 3 for March.
Well, just pass an INT parameter to your stored procedure (I'm assuming you're using this in a stored procedure?). Then you can do something like
IF DATEPART(dd, CURRENT_TIMESTAMP) = #givenday ...
Does that help?
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())