T-SQL stored procedure date variable - sql-server

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.

Related

SSRS date range parameter

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())

Data Issue with Oracle NVL() and SSRS/Visual Studio 2015

I have an SSRS report that has the option of running for two date ranges.
If it's run adhoc from the Report Manager in a web browser, the user can specify a start date and stop date in the date drop downs.
For Report subscriptions, there are hidden parameters that specifies what the dates should be depending on the parameter. This can be today's date, last week, etc.
In the report data query we use:
between nvl(:i_start_date,:subscription_start_date) and nvl(:i_stop_date,:subscription_stop_date)
This worked in SSRS 2012 and Visual Studio 2010. However, I am receiving the following error in SSRS 2016 and Visual Studio 2015:
ORA-01830: date format picture ends before converting entire input string
If we remove the nvl(), or use a non-parameterized value in nvl, the problem does not occur. For example:
and cr.contact_date between :i_start_date and :i_stop_date -- works, but only for report manager web runs
and cr.contact_date between :subscription_start_date and :subscription_stop_date -- works, but only for subscriptions
and cr.contact_date between nvl(:i_start_date,sysdate) and nvl(:i_stop_date,sysdate) -- works, but obviously not using the parameter date.
Why does using nvl() cause this date picture error? It's as if the Date/Time type is not passed into the Oracle query properly from SSRS.
All date parameters are set to Date/Time and the subscription parameters return a value that has been converted to date using CDate().
Here's the code for the subscription start parameter. The subscription stop is similar:
=CDate(switch(
Parameters!SubscriptionParam.Value=0, DateSerial(1990,1,1) ,
Parameters!SubscriptionParam.Value=1,today(),
Parameters!SubscriptionParam.Value=2,dateadd("d",-1,today()),
Parameters!SubscriptionParam.Value=3,dateadd("d",-7,today()),
Parameters!SubscriptionParam.Value=4,DateSerial(Year(today()), Month(today()) - 1, 1)
))
0 = Free, 1 = Yesterday, 2 = Today, 3 = Last Week, 4 = Last Month.
The dates returned are correct.
I could not find anyway to make the previous code work so I converted the code to the following:
and (
(:SubscriptionParam = 0 and cr.contact_date between nvl(:i_start_date,'01-JAN-1900') and nvl(:i_stop_date, sysdate))
or
(:SubscriptionParam <> 0 and cr.contact_date between nvl(:subscription_start_date, '01-JAN-1900') and nvl(:subscription_stop_date, sysdate))
)

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)

How do I establish a parameter in SSRS report

I am new to SSRS.
I have a dataset that pulls in values from a table and displays them in a report.
One of these columns in the dataset is the "Date_Recieved" column.
I want to add a "start" and an "end" [calendar date selector] that will filter the rows displayed on the report if they fall between the "Start Date" or "End Date".
In other words I want to filter the rows displayed in the report based on whether the value they contain for the "Date_Recieved" column falls between the start/end date that the user has selected.
How is the dataset being populated? Raw SQL or a View or a Stored Procedure? as this will change how the solution will be?
If you are using a Stored Procedure.
You will need to setup two parameters #startDate and #endDate.
The stored procedure will have two parameters and you pass these into the data set via the parameter tab on the dataset menu.
If you are using a View or RAW SQL you will need to add a where clause to the SQL like such:
Where cast(Date_Recieved as date) between #parameter1 and #parameter2
then in the parameters section of the dataset options you will see these two appear and populate them with the #start and #endDate.

SSRS changing values based upon other values

I have ran into a problem with my report.
This drop down needs to adjust the values in the PeriodStart/PeriodEnd
When the Weekly Period is changed, these values need to change. Although, I want to keep the default values before Weekly Period is changed.
So lets say that the drop down has its value as 10/1/12 | 10/7/12. I am assuming I will change the expression of the two date fields and do some kind of split on the drop down value, but I am not sure what that would be. This is where I need guidance. Thanks
I am not near Report Builder right now, but it should go something like this:
1) make your weekly period drop down a datetime field with the value of the start date, and the label in whatever format you like.
2) set your period start date parameter to be a datetime field, and in the Default Values tab, set the default value to be the expression =Parameters!WeeklyPeriod.Value
3) set your period end date parameter to be a datetime field, and in the Default Values tab, set the default value to be the expressions =DateAdd("d", 7, Parameters!WeeklyPeriod.Value)

Resources