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

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

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.

Day/Month/Year date changed to Month/Day/Year in SQL

I have an old Classic ASP application that was worked perfectly, recently it was moved to window server 2012 server with IIS 8.5, the problem is that I have a form to add record with Day/Month/Year format, when the date entered as 23/06/2015 in SQL will be added correctly as 23/06/2015 but when date entered as 01/06/2015 : 1st june 2015 in SQL will be added as 06/01/2015 Month/Day/Year which will be incorrect
I checked the location and date format for the server and it was dd/mm/yyyy, and the database didn't changed since the old server SQL 2008.
I searched the net and found the i need to change the IIS culture under GLOBALIZATION, but I don't know what to choose even if I chose my country still I am facing the same issue.
If you don't want to get into changing globalization and localization settings, the simple answer is to use the universal date input YYYYMMDD when inserting dates as strings.
Update
To expland on your comment: when changing the format of the date string in an insert/update query it does not change the format within the sql table itself.
e.g.
UPDATE [table]
SET [column] = '20150716 10:22'
WHERE Id = 7489
When viewed displays the same as the other records in the table:
Try this:-
https://www.webwiz.co.uk/kb/asp-tutorials/date-time-settings.htm
Add an entry into global.asa as below...
'When a session starts on the server the following code will be run
Sub Session_OnStart
'Set the server locale
Session.LCID = 2057
End Sub
Somethimes when nothing fix this problem (or a global solution could damage old scripts), A workaround that you could use is the convert function, to change the format in your stored procedure.
For Example:
select getdate() --prints 2018-10-05 12:24:43.597
select Convert(char(10),getdate(),103) --To force the date to be dd/mm/yyyy
And not leting the asp page to format the date, just print raw.

Date parameter using with stored procedures in SQL Server

I'm trying to retrieve data in iReport using stored procedure(SP) in SQL Server 2008.
My stored procedure uses two parameters Date_From & Date_To. I would like to run the report in specified (by these parameters) period but in any case I have info like "No data in report". In table the date is stored in format "yyyy-MM-dd 00:00:00.000". both of the parameters are set as "java.util.Date". In the Report query I have:
Query language = plsql
In query window: Name_of_procedure $P{Date_From}, $P{Date_To}
I found somewhere info that is possible to change the Date format and I have change it to:
Date format "yyyy-MM-dd", but after this change, instead of generate the report iReport is coming back to the Designer window, without any error or info.
May I ask for help, how to correct set the parameter that would provide the Date in format "yyyy-MM-dd"?
iReport version: 5.1.0
To see the error you can go to Window--> Report Output option there you can find the error.
To call the procedure syntax should be
CALL Name_of_procedure ( $P{Date_From}, $P{Date_To})
and to set the default value for parameters you can use this syntax.
new Date(114,7,06) --> to set default date as 06 Aug 2014.

Query works in Query Designer but not in MS SQL Server Report Builder

While using the Microsoft SQL Server Report Builder, I am able to execute a query in the Query Designer and retreive the result. The query has a where clause on dates.
select * from table where DB_timestamp > to_date(:StartDate, 'mm/dd/yyyy') and DB_timestamp < to_date(:EndDate, 'mm/dd/yyyy')
On execution, I get a pop up where I enter the two variables (StartDate & EndDate) in the given formats. Now when I try to run the same for generating a report, I have to select the two variables using a calender picker. Altough the dates show up in the desired format, I always get the below error:
An error occured during local report processing.
Query execution failed for dataset 'XYZ'.
ORA-01843: not a valid month
What can cause the query to run successfully in one place but throw an error when the variables are selected by the calender picker?
There mught be some kind of culture clash going on. If SSRS is sending dd/mm/yyyy and your db is expecting mm/dd/yyyy things will not work as expected or even crash on certain dates.
I would remove the to_date function from the where clause, e.g.
where DB_timestamp > :StartDate and ...

Search By Date in SQL Server 2012

We just upgraded to SQL Server 2012 from 2005. While I'm a novice, something this simple couldn't be this difficult. I used to be able to pull data from a table based on the date vs date and time. As it now stands I have:
Select * from receipts_table where receipt_cancel_date = '2013-09-20'
before we upgraded this would work fine. How can I run this and actually get the desired results as I know there's receipts with a cancel date of 2013-09-20.
Thanx
If you are passing string for a date parameter, best format is ISO (yyyymmdd) format. Otherwise even though your string work in some servers it might not work in another depending on the culture of the server. ISO format is culture independent.
Also remove the time part from receipt_cancel_date column by converting it to a DATE (if DATETIME) for comparison purpose.
Try this:
Select * from receipts_table
where convert(date, receipt_cancel_date) = convert(date,'20130920')
Or use 120 style with your format:
Select * from receipts_table
where convert(date, receipt_cancel_date) = convert(date,'2013-09-20',120)

Resources