SSRS adding default time to a date parameter - reporting-services

I have an SSRS report with two parameters that are datetimes. Of course the date selector doesn't allow you to select times to go with the dateTIME, that would make too much sense. So my idea was to give the datetime parameters a default time. For example, if I wanted the default value of a parameter to be today's date at 8:30 AM, how would I do that?
So if today was 9/4/2013 I want to see exactly this: 09/04/2013 8:30 AM.
I have tried all kinds of formatting. The closest I got was doing this:
=CDate(Format(Today(), "MM/dd/yyyy") & " 8:30 AM")
But I have never been able to get the seconds to not show up because you always have to convert this back to a datetime from a string, otherwise you get an invalid type error, and CDdate ALWAYS displays the seconds.

It seems like you're trying to format the date directly in the parameter's default value, is that correct? The thing is, CDate() converts a string into a DateTime; and DateTime objects do have seconds. If you don't want to display those seconds in your report, you should convert the date into a formatted string, such as:
=Format(Parameters!yourParameter.Value, "MM/dd/yyyy hh:mm")
In order to set a default time to your date parameter, you could also use something like this in the parameter default value:
Today().AddHours(8).AddMinutes(30)

I tried everything I could find on SO to format these dates but couldn't get it to drop the zero's in the parameter as a date field or just appear blank. I was using SSRS 2005 so was struggling with its clunky / buggy issues.
My workaround was to add a column to my custom [DimDate] table in my database to pull dates. I added a column that was simply a string representation of the [date] column. I then created 2 new Datasets that pulled in the following queries for 2 defaults for my 'To' & 'From' date defaults -
'from'
SELECT Datestring FROM dbo.dimDate WHERE [date] =
(SELECT max(date) FROM dimdate WHERE date <= GETDATE() )
'to'
SELECT Datestring FROM dbo.dimDate WHERE [date] =
(SELECT max(date) FROM dimdate WHERE date < DATEADD(month,-3,GETDATE()))

Use this =DateAdd("s",-1,DateAdd("d",1,Parameters!dateTo.Value)) works for me adjust your time by changing to hours.

Related

Have a table that includes startdate and durationminutes. Trying to get that to give new end date in SQL

Title says it all.
Table looks like this
Table: Time
First Column: startdate (shown as yyyy-mm-dd hh:mm:ss)
Second COlumn: durationminute (shown in minutes)
Since the above doesn't give me an end date, I'm trying to create a query which will do that.
I'm assuming this is going to be a dateadd function but I can't get it to work. The durationminutes is a variable to is dependent on that one row so date_add(minutes, 30, startdate) wouldn't work as there is no constant.
I've tried
date_add(days, durationminutes/1440, startdate)
But am getting a syntax error.
MySQL understands date arithmetics. Assuming that startdate is of date or datetime datatype (as it should be!), you can do:
select t.*,
startdate + interval durationminute minute as enddate
from time t

SSRS: No Data Returned when Date Prompts used

If I use this in my dataset SQL:
DECLARE #StartDate DATE = '2018-01-01'
DECLARE #EndDate DATE = '2018-03-01'
The report runs and returns the expected Data.
When I comment out the #StartDate & #EndDate variables - delete the two parameters from the Report - and run the report using Date Prompts at run time [using the same Dates] - I get no data returned.
The Date field that I am attempting to filter on is a Datetime field.
I have tried the following two approaches in my SQL:
o.ORDERDATE >= #StartDate And o.ORDERDATE <= #EndDate
cast(o.ORDERDATE as DATE) >= Cast(#StartDate As Date) And cast(o.ORDERDATE as DATE) <= Cast(#EndDate As Date)
No data.
I added a Text Box to the Report Header and put the #StartDate Parameter value in there and got this: 01/01/2018 12:00:00 AM at run time.
I have gone back and forth a few times between uncommenting and commenting the Date variables at the top of the SQL. When I use the local variables - I get data. When I use the Date Parameters - no data.
I use Date Prompts in many of my other reports with no problems. I will go back and see if any of them is on this particular table and this particular date field - or if any of the other dates are Datetime fields ....
Meanwhile, I would appreciate any suggestions.
Thanks!
I would suspect a conversion issue comparing Date and DateTime types. To prove this, set the dates manually in your query to the full date and time. if this produces no results then try something simple like.
WHERE CAST(o.ORDERDATE AS Date) Between #StartDate AND #EndDate
Bear in mind that if you start your date with the year (I think) by default this is interpreted as being in YYYY-MM-DD format.

SSRS Report subscription date parameters

The thing that I want to do is as follows
I have a SQL reporting services report that takes date parameter (I have two parameters as Run_Report_From_Date_PARM and Run_Report_To_Date_PARM. These dates are picked from the Datepicker). I want to create a subscription to the report that will run on 1st of every month and show the data for the prior month.
While making subscription if i choose 2016-08-01 for Run_Report_From_Date_PARM and 2016-09-01 for Run_Report_To_Date_PARM then the report is generating always with that date range only. But i need the date ranges modify automatically after every month(like for the next month From date should be 2016-09-01 and to date should be 2016-10-01 ).
What is simplest is to just use expressions to set the default values for these parameters.
For a start date of the first of last month:
=DateAdd("m", -1, DateAdd("d", 1 + -1 * DatePart("d", Today()), Today()))
For an end date of the last of last month:
=DateAdd("d", -1 * DatePart("d", Today()), Today())
However, if you don't want these to be the default values when executing the report, you can set up a hidden boolean parameter that you only set to true in the subscription settings. That can be useful if you need to include formatting or default value tweaks in the subscription version of a report.

How to convert a date field to a date time format? SQL 2008

The conversion of a date data type to a datetime data type resulted in an
out-of-range value.
The statement has been terminated.
I just need to convert a date field to a date time format.
Example of what the code would look like to convert from date to datetime2
DECLARE #d1 date;
SET #d1 = GETDATE()
-- When converting date to datetime the minutes portion becomes zero.
SELECT CAST (#d1 AS datetime2) AS [date as datetime2]
For more information about cast and convert, see the Microsoft reference.
You probably get out of range exception because Date supports a date range between
January 1, 1 and December 31, 9999 , while DateTime supports a date range only between
January 1, 1753 to December 31, 9999.
Want to know Why 1753? Read this. (Recommended reading for anyone that likes trivia items)
Try converting to Datetime2 instead of Datetime, this should be OK since Datetime2 supports a date range similar to date.
the conversion can be done simply by using CAST:
SELECT CAST(YourDateTypeColumn As Datetime2)
FROM YourTable

SSRS DateTime format the paramaters

I have two datetime parameters in my report (from date and to date).
Cannot seem to get the formatting to work though.
The default for FromDate should be yesterday (19/01/2015 00:00:00) and the ToDate should be yesterday (19/01/2015 23:59:59)
Set the parameter type of both parameters to Text and use the following expressions to set the values:
= FormatDateTime("01/19/2015 00:00:00") <---- For 'From Date`
= FormatDateTime("01/19/2015 23:59:59") <---- For 'To Date`
dd/mm/yyyy format seems to throw an error while mm/dd/yyyy doesn't.
The above is for hardcoded vales. Adding a script for dynamic in sometime.
EDIT
Below is the solution to meet your requirement:
Keep your parameter type as Date/Time and use the following expressions to set the default value
ENDDATE =
=dateadd(
dateinterval.Second,
-1,
CDate(today)
)
STARTDATE=
=dateadd(
dateinterval.Day,
-1,
CDate(today)
)