Can we show Between date parameters displayed like below scenario - reporting-services

In my report contain FromDate and Todate Parameters,
but i need to display as drop down list below screen
When user clicks today it displays as today data,select IsBetween data displays as between data,
select this month displays as This month data like that needed.
Can anyone explain me How can i achieve this above type scenario in SSRS?

One way to do this is to pass the selected date description to your dataset query and have the appropriate dates calculated there before running the select statement. So the query would look something like this:
declare #StartDate date
declare #EndDate date
if #Date = 'Today'
BEGIN
#StartDate = GETDATE()
#EndDate = GETDATE()
END
SELECT *
FROM A
WHERE Date between #StartDate and #EndDate
And you would have to make sure you are passing the value of the Date parameter to the dataset like this:

Related

How to add Date and Time Parameter for two different days in SSRS

I am using SSRS 2012. I have an issue with my date and time parameters.
I in my dataset I have [datetime], [date] and [time] columns, and parameters #fromDate, #toDate of datetime data type and #fromtime, #totime of text data type.
It works perfectly until the user chooses the the #totime to be less the #fromtime.
Example :
#fromDate = 10/1/2019
#toDate = 11/1/2019
fromtime: 20:00 (which is 10/1/2019)
totime: 06:00 (which should be the next day 11/1/2019)
I get no data in the report and I think because the system understand it as swapping the data which is not equivalent with the query.
Where
F.[Datetime] >= #fromDate
AND F.[Datetime] <= #toDate
AND Em.Name IN (#Name)
AND F.[time] >= #fromtime
AND F.[time] <= #totime
I expect the output to be :
5 row for employee names with the mentioned date and time who get out from 10/1/2019 20:00 to 11/1/2019 06:00 am .
but the actual output was no data.
You need to have 2 parameters, #fromDate, #toDate with datetime data type.
Your query still valid:
When you input #toDate and #fromDate use the following format: dd/mm/yyyy hh:mm:ss
using your previous example input for #toDate would be: 10/01/2019 20:00:00 and input for #toDate would be: 11/01/2019 06:00:00
I have used this statment and worked perfectly.
where datetime >= DATEADD(dd,DATEDIFF(dd,0,#StartDate),#StartTime)
and datetime <=DATEADD(dd,DATEDIFF(dd,0,#EndDate)+1,#EndTime)
Regards,
Jamila

SQL Query Between Dates in Visual Studio Table

I have a table within Visual Studio and I am wanting to create an input field in for a StartDate and another input field for an EndDate where a user selects a date for both and it will return all records that have a date that falls between the Start and End dates that the user chooses. I would also like the user to have the option to only search for the StartDate and leave the EndDate null so essentially only having 2 input fields for the table (or 1 if End Date is null).
I have this right now which returns the date the user selects but only on that date:
(Date = #Date) AND (#Date BETWEEN #StartDate AND #EndDate)
#StartDate is a date that comes before any date in the DB. #EndDate is the current day. This is just in case a user types in a date that is not within the date range of the database and it will return an error.
I have tried making the parameter #StartDate equal to #Date so when the user selects a date, it will make that the starting date. I also have the input field for the user to select an EndDate which seems to work. I just can't get the start date parameter to work like I want.
These are the queries I have tried:
1.
(Date = #Date) AND (#StartDate = #Date) AND (#Date BETWEEN #StartDate AND #EndDate)
2.
(Date = #Date) AND (#Date BETWEEN #Date AND #EndDate)
How do I modify this query/parameters to get the user to select a start date and an end date in input fields and for the table to return all the records that are between those dates?
(Date = #Date) AND ...
This will always require Date to equal #Date to be true, regardless of the rest unless some OR ... comes along.
Assuming the two fields fill #StartDate and #EndDate (to me it isn't clear, why there is a third variable, #Date) you're probably searching for something like:
#EndDate IS NULL
AND Date = #StartDate
OR Date BETWEEN #StartDate
AND #EndDate
If #EndDate IS NULL (the user hasn't put anything in the respective field) Date = #StartDate has to be true. Otherwise Date BETWEEN #StartDate AND #EndDate (which won't ever be true if #EndDate IS NULL just in case you wonder).

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.

How to use DATENAME in WHERE clause

Am using Sql Server 2008, I have a column named Date in my table, and I want to get the datas for the particular date.... I need to give this Date in my WHERE condition.
for example, if I want to get the records for the particular month in the given date, how can I use this Date in WHERE condition.
DATANAME(MONTH,'#Date')
if I give like this in my query I can get the month from the given DATE, the same way I tried by putting in WHERE condition like,
WHERE DATE= DATANAME(MONTH,'#Date')
here it reports conversion error...how can I display the datas for a particular month, can anyone help me
If you want a month of data for a table you should check against an interval. The query is not able to use indexes on the date column if you are applying functions on the column.
Use something like this to get data for April 2012.
-- The date parameter
declare #Date datetime
set #Date = '2012-04-11'
declare #FromDate datetime
declare #ToDate datetime
-- set FromFate to first of april
set #FromDate = dateadd(month, datediff(month, 0, #Date), 0)
-- set ToDate to first of may
set #ToDate = dateadd(month, 1+datediff(month, 0, #Date), 0)
select *
from YourTable
where [Date] >= #FromDate and [Date] < #ToDate
If you want to show data for a particular year and month you can use the YEAR and MONTH functions:
SELECT ...
FROM ...
WHERE YEAR(mydate) = 2012 AND MONTH(mydate) = 3 -- March, 2012
To me it seems that your field Date is not of type varchar or nvarchar, so using a condition where a Datetime = string is obviously wrong.
Have you tried
WHERE DATE= #Date
Shouldn't it be:
DATENAME(MONTH, #Date)
Instead of:
DATANAME(MONTH,'#Date')
(Notice "DATA" vs "DATE" and #Date isn't in quotations)
Then to use this against a date/datetime column you would have to cast both sides like below:
WHERE datename(Month, [Date]) = datename(Month, [Date])
Warning: The above does not use any indexes so isn't as efficient as "WHERE Date = Date"
First: Remove '' from variable. #Date, not '#Date'
If you want to find dates from specific month. (You have to remember about year condition also)
WHERE DATANAME(MONTH, #Date) = 'April'
if you want to find exact date:
WHERE DATE = #date

Search between dates and times in SQL Server 2008

I need to search between dates and times.
For example, between date: 30/02/2007, time: 10:32 and date: 21/06/2008, time: 14:19
What is the most simple query for this?
Thanks in advance.
you should look at the date time formats available in SQL Server: http://msdn.microsoft.com/en-us/library/ms187928.aspx
yyyy-mm-dd hh:mi is what you should use:
try:
SELECT
*
FROM Records
WHERE DateCreated>='2007-02-30 10:32' AND DateCreated<='2008-06-21 14:19'
in the above query the strings will be converted to datetime data type if DateCreated is a datetime column. and the query will work.
you can create local variables of datetime data type and use a query like:
DECLARE #StartDate datetime, #EndDate datetime
SELECT #StartDate='2007-02-30 10:32', #EndDate='2008-06-21 14:19'
SELECT
*
FROM Records
WHERE DateCreated>=#StartDate AND DateCreated<=#EndDate
I like using <, <=, >=, or > because it allows more flexibility than BETWEEN and forces you to think about including endpoints or not.
Another thing to consider is getting all data from a complete day:
DECLARE #StartDate datetime, #EndDate datetime
--set the days you want
SELECT #StartDate='2007-02-30 10:32', #EndDate='2008-06-21 14:19'
--remove the time
SELECT #StartDate=DATEADD(day,DATEDIFF(day,0,#StartDate),0), #EndDate=DATEADD(day,DATEDIFF(day,0,#EndDate),0)
--get everything on '2007-02-30' up to the end of the day on '2008-06-21'
SELECT
*
FROM Records
WHERE DateCreated>=#StartDate AND DateCreated<#EndDate+1
Try this:
SELECT
*
FROM
Records
WHERE
DateCreated BETWEEN #Date1 AND #Date2