SSRS date range parameter - sql-server

I have a report in SSRS 2016 that contains one SP which has a date field called startdate. I'd like to create a parameter where the user can select between two ranges: startdate >='2017'or startdate < '2017'. This seems like it should be simple, but I can't see to find an easy way to do this.
I've tried to create a parameter in SSRS where I chose "Specify Values" and manually added these values but I get an error that the "expression references the parameter "startdate" which does not exist in the Parameters collection.
I'm sure I can create this by creating a stored procedure with these dates, but that seems like more work than is needed.
Does anyone know of a better way to do this?

If you are looking to have a parameter that has two options, 0 - Before 2017, and 1 - After 2017 then you should just create a Date parameter which has two options, Before 1/1/2017 in the label field with a 0 in the value field and After 1/1/17 in the label field with a 1 in the value field. Then in your report you just have to filter your data based upon the 1 or 0 value.
For example:
DECLARE #DateFilter INT = 1
IF ( #DateFilter = 0 )
BEGIN
SELECT * FROM dbo.MyTable t
WHERE t.DateFilter < '1/1/17'
END
IF ( #DateFilter = 1 )
BEGIN
SELECT * FROM dbo.MyTable t
WHERE t.DateFilter >='1/1/17'
END
I don't know why you would filter your data this way. I recommend to use Larnu's suggestion that you have two parameters, a Start Date and an End Date. Otherwise this could return a lot of rows for the second option as time goes on.

Add a couple of parameters in SSRS say, FromDate and ToDate and make its data type as Date/Time.
You can specify the default values for these parameters using DateAdd functions and samples give below:
Today:
=Today()
last week from today:
=DateAdd(DateInterval.Day, -7,Today())
You can add/reduce year, quarter month etc. in a similar way as shown below. Just change the number to the required length
=DateAdd(DateInterval.Year,-1,Today())
=DateAdd(DateInterval.Quarter,-1,Today())
=DateAdd(DateInterval.Month,-1,Today())
=DateAdd(DateInterval.DayOfYear,-1,Today())
=DateAdd(DateInterval.WeekOfYear,-1,Today())
=DateAdd(DateInterval.WeekDay,-1,Today())
=DateAdd(DateInterval.Hour,-1,Today())
=DateAdd(DateInterval.Minute,-1,Today())
=DateAdd(DateInterval.Second,-1,Today())

Related

T-SQL stored procedure date variable

I have this T-SQL stored procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE dbo.datealter
#LastChangeDate AS date
AS
BEGIN
SELECT
s.[DivNo]as Division,
SUM(CASE WHEN s.Date = #LastChangeDate
THEN s.SELLINC ELSE 0 END) ActualSales
FROM
[dbo].[NewLayerReports] s
WHERE
s.BRN = 1
GROUP BY
s.[DivNo]
END
GO
When I execute this it works ok.
I am using this stored procedure in a SSRS report. When I execute the SSRS report, it asks me to enter or pick the date.
Here always I wanted a calendar to show last year current month. For example, If I run the report on November 2016 the calendar should show Nov 2015.
How can we do this please?
In SSRS parameter, if I set default value then, I couldn't edit the date value. Please see the picture,
You can give default value to your parameter. Give expression in your default value for parameter as Date.Now.Date.AddYears(-1). It will show same month in previous year when you run the report.
If you want to set the default value of a parameter in SSRS, right click on the Parameter and select Properties. In the Default Value screen you can set the expression that returns the date.
To get this day last year, you can use:
=today.AddYears(-1)
Though if you want the start of the month you also need to add the days as well:
=today.AddYears(-1).AddDays(1-today.day())
If appears you just want to display the date relative to the one the user picked. In that case you can use the above expressions as required within a report element (such as a title at the top of the report), but replace any instance of today with Parameters!ParameterName.Value.

SSRS optional drop down parameter passing dates

I currently have a set of drop down parameters that pass dates to a query. There is a display value available to select for each Work Week.
Create Date Parameter:
Display in the drop down: 2016 01 (1/3/2016) P01-16 Q1-16
Value being passed to the SQL is WEEK_BEGIN_DATE: 1/3/2016
New Ship Date Parameter:
Display in the drop down: 2016 01 (12/27/2016) P12-15 Q4-15
Value being passed to the SQL is SHIP_WEEK_BEGIN_DATE: 12/27/2016
I would like to be able to make both of these optional.
I tried the following in the parameter value SQL to get the parameter to allow nulls but I got a data type error. I did start going down the cast as varchar() route but it was getting messy quick.
SELECT 'NULL' AS WEEK_BEGIN_DATE
UNION
SELECT d.WEEK_BEGIN_DATE FROM DATE d
Any suggestions are much appreciated. Let me know if I can provide any other helpful info.
this should work, you don't need the single quotes
SELECT NULL AS WEEK_BEGIN_DATE
UNION
SELECT d.WEEK_BEGIN_DATE FROM DATE d
to make parameters optional you could in where clause write something like
where (WEEK_BEGIN_DATE = #date or #date is null)
If your parameter of dates was a VARCHAR() you would need to do what Kostya stated. But since it's not, you can click "Allow null value" under Report Parameter Properties (rightclick on your parameter and select properties)

Run SSIS Package skipping Invalid dates

I've a DWH Table which has Date Value stored as int.Now I wanna get all records for a day which are active on a day with conditions as start_date<=#date and End_Date>#DATE
for almost close to 2 years.
I've used a for loop and 3 variables where in hardcoded FROM_DATE and TO_DATE as 20130101 and 20150107 and the 3 variable VAR_DATE initially set TO_DATE and decreasing by 1 until it reaches FROM_DATE.
But after reaching 20150101...It's starting to insert values for 20150100,20150099 and so on..
Is there a way where in I can check to see if the VAR_DATE column being used is actually a valid date(by convert and comparing on the go) or any possible way of using a result set..
Thanks,
Vijay
Use a script to convert the integer into a string. Then parse it into a date object like so:
DateTime parsedDate;
bool parseResult = DateTime.TryParseExact(dateValue, "yyyyMMdd", null, DateTimeStyles.None, out parsedDate)
Then parseResult will be true if it's a valid date.

SQL query to search

i am new at sqlserver and i just started learning it , and i have a question .
**i want an SQL query that can bring up all rows that were added in the past 6 months. in table 'users' the date is in field 'joined' but its in UNIX time stamp format
**and i want then like another SQL query the same as above but it also with the results sets the field 'activate' in each row to 'yes'
i tried that code :
select * from users where time_of_post is like '07/**/2011'
** = anyday but i can`t implement it right .
Thank you a lot for your help in advance .
select *
from users
where datediff(mm, time_of_post, getdate()) <= 6
What you want to use is the DATEDIFF() function, and get the difference between today's day (GETDATE()) and the stored dates (time_of_post). If that is less than or equal to 6 months (as denoted by the first parameter, mm) then that should be what you're looking for.
EDIT:
If you're looking to use this logic for an UPDATE, you'd do something like this:
update users
set activation = 'yes'
where datediff(mm, time_of_post, getdate()) <= 6
and activation <> 'yes'

Allow Search for Invalid Date stored as string - Is it recommended or not

I have two tables for an Entity - say Valid_Doc and Invalid_Doc. If document is valid, then all the data gets saved in Valid_Doc table.In case any of the attribute of document is invalid , it gets saved in Invalid_Doc.Due_Date is on of the column of both the tables. In Invalid_Doc , we are saving Invalid dates as string.
Suppose if user searches for documents through a SEARCH screen with following date
Due_Date - is after - 07/07/11,
Shall we show all the documents from both the tables.As Due_Date in Invalid_Doc table is string, there is no way we can compare the entered search date with the dates available in database in Invalid_Doc table.
Can someone please guide me whether to use DATEDIFF - i.e. need to convert the String date in DB to Date(millisecs) first and then do the comparison with the entered data.Doing this , there may be unpredictable results. So , shall we allow the user to search for Invalid doc through Date or NOT.
Select * FROM invalid_doc iil WITH (nolock) WHERE
CAST(Datediff(s, '19700101 05:00:00:000', iil.due_date) AS NUMERIC) *
1000
BETWEEN '1120501800000' AND '1120501800000'
Where '1120501800000' and '1120501800000' are Date converted in milliseconds.
Please suggest.
I would convert the dates in the database to a uniform string format using regex (eg. Q20011231Q) and also convert the query to the same format before searching.
This way you would have control on your data and can easily do comparisons
I continue as answer :)
As I don't know your language you handle the xml data I strongly suggest you validate you date befor you insert into your database.
It is not possible to enter a non valid data value into a datatime field.
if you have a data like 11/24/2011 and insert it, the datetime value always add the time itself.
Then you have eg. 11/24/20011 17:29:00 000 as value stored.
If you insert less then a date it might crash or if the value can be converted to a valid date, the missing parts will be replaced by "current date information".
So in fact you have to validate you string, convert it. Something like this:
-- #datestring <= somehow your string from xml
SET arithabort arith_overflow OFF
SET #date = CAST(#datestring AS DATETIME)
IF #date is NULL SET #date = GETDATE()
SET arithabort arith_overflow ON
You turn overflow error mode off, try convert and set default if it fails.
Or in MS SQL
IF ( ISDATE(#date_string) = 0 ) SET #date = GETDATE()
Hope this helps

Resources