SSRS DateTime format the paramaters - reporting-services

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

Related

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.

MySQL STR_TO_DATE returning null

I am trying to convert dates in the format mm/dd/yyyy to the standard date format yyyy-mm-dd using the STR_TO_DATE function. Some fields in my date column are null and some contain a date.
For instance, 8/22/2011 should become 2011-8-22.
When I select my date column, it looks like this:
8/22/2011
8/10/2010
5/12/2012
etc.
I tried using the code
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y')
which filled the column with NULL values. Also tried
UPDATE table SET date = STR_TO_DATE(#date, '%m/%d/%Y')
with same result, although this time I did not get a warning message.
The first one is correct:
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y')
But if the date is not valid (not in %m/%d/%Y format) then it returns NULL
Try executing and then showing warnings. It tells you what is wrong:
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y');
SHOW WARNINGS;
Maybe some dates are not in format %m/%d/%Y (posibly %d/%m/%Y)

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

MySQL date validity

Does MySQL provide any function which verifies the validity of a date? The DATE function returns NULL upon provision of the invalid date 2013-02-30 for example. However, I am also using STR_TO_DATE simultaneously, which mysteriously stops DATE from working correctly.
SELECT DATE('2013-02-30'); NULL
SELECT STR_TO_DATE('2013-02-30', '%Y-%m-%d'); NOT NULL
SELECT DATE('2013-02-40'); NULL
SELECT STR_TO_DATE('2013-02-40', '%Y-%m-%d'); NULL
SELECT DATE(STR_TO_DATE('2013-02-30', '%Y-%m-%d')); NOT NULL
Why does STR_TO_DATE halt DATE's functionality and is there some workaround to verify if a date is valid when using STR_TO_DATE (which I am obligated to use)?
I have stumbled upon the answer in the meantime: apparently the DATE function skips a few validation checks, when the data type is already that of 'date' (STR_TO_DATE converts strings to date data types). Therefore, converting the date to a string after having parsed it to the correct format with STR_TO_DATE, does the trick:
#valid_date = NOT ISNULL(DATE(CONVERT(STR_TO_DATE('2013-02-29', '%Y-%m-%d'), CHAR))).
It is very difficult to verify if a field is a date because of all the different possible date formats that would need to be taken into account. BUT if you know that the field date formats are one of these:
'yyyy-mm-dd'
'yyyy-mm-dd hh:mm:ss'
'yyyy-mm-dd whatever'
This code will help you:
SELECT count(*) FROM `table`
WHERE DATE(STR_TO_DATE(`column`, '%Y-%m-%d')) IS NOT NULL
AND `column` NOT REGEXP '^[0-9\.]+$'
Basically :
the first condition tells you if is a date, but unfortunately doesn't exclude numbers (ex: DATE(STR_TO_DATE(**1**, '%Y-%m-%d')) = '2001-00-00'
the second ensures that numbers are excluded, which leaves you with dates only that follow the formats above.
If count(*) is >0 then it's a date, if it is 0 it's something else.
Note
This method works for strings of any date format as long as you know in advance what format they follow (which I know is not always the case but still helpful). Just replace the format a priori in STR_TO_DATE
i can't understand your purpose clearly, maybe this is a idea;
SELECT DATE_FORMAT(yourFiled, '%Y-%m-%d') days FROM yourTable GROUP BY days;
this is not null; you can change it. some like
SELECT DATE_FORMAT(yourFiled, '%Y-%m-%d') days FROM yourTable WHERE yourFiled > '2013-9-23 00:00:00' GROUP By days;
Try this:
SELECT DATE(STR_TO_DATE('2013-00-30', '%Y-%m-%d')); --is also NOT NULL

SSRS adding default time to a date parameter

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.