Datediff function adds startdate day also to the output - reporting-services

I have a report in Report Builder 1.0 wherein I have written a formula that calculates the number of days between startdate and enddate. For that I have used a DATEDIFF function in such a manner:
DATEDIFF(DAY, TODAY(), column from table which has end date)
Suppose, if today's date is 1 July 2013 & the column is sending end date as 10 July 2013, then the DATEDIFF function is returning "10 days" whereas it should return output as value "9 days". Why is it returning 10 days rather than 9?

Execute this in Sql Server and you get 9 days:
select DATEDIFF(day, '2013-07-01', '2013-07-10')
Using this as a dataset:
select CAST('2013-07-01' AS DateTime) AS StartDate, CAST('2013-07-10 23:59:59' AS DateTime) AS EndDate
then using this expression in SSRS:
=DateDiff(DateInterval.Day, Fields!StartDate.Value, Fields!EndDate.Value)
still yields 9 days, even when using a time component. Try the following expression:
=DateDiff(DateInterval.Day, DateTime.Today, Fields!EndDate.Value)
Are you displaying the date fields to make sure what you think you should get is what you actually get? For instance, make sure you aren't being returned cached data.

Related

SSRS Returning incorrect WW

I have an SSRS report (2008R2 3.0) that uses parameter filters for workweek in the format YYYYWW. In 2018 there were 52 work weeks based on the ISO standard (source) .
For default parameter value I select the last WW using the following formula:
DATEPART(DateInterval.Year, DATEADD("d", -7, now())) & FORMAT(DATEPART(DateInterval.WeekOfYear, DATEADD("d", -7, now())), "00")
For some odd reason this returned 201853. From what I understand it should be 201852 given the date when ran was 20180106.
I know in T-SQL I can define return type using ISO_WEEK but is there an equivalent for DateInterval in SSRS?
To simplify:
DATEPART(DateInterval.WeekOfYear, DATEADD("d",-7,now()))
Returns 53 instead of 52 when ran from 201801-201807.
Any idea as to why this is returning the wrong WeekOfYear value?
I think this may be due to your Regional First Day of the Week setting on your server.
If your First Day of the Week is Saturday, there are 53 weeks in the 2018. You can set the first day of the week with the DATEFIRST setting in SQL Server.
Use SELECT ##DATEFIRST to determine your servers setting.
This example code will show the difference.
SET DATEFIRST 7
SELECT ##DATEFIRST
SELECT DATEPART(WEEK, '2018-12-30')
SET DATEFIRST 1
SELECT ##DATEFIRST
SELECT DATEPART(WEEK, '2018-12-30')
For your query, you may need to set the first day of the week to Sunday with SET DATEFIRST 1.
MS Docs: DATEFIRST

Get all dates for a specific day of week (e.g. Monday) between 2 dates

Consider given 2 dates between 2015-01-01 and 2015-01-30, I need to find the dates for every Monday and every Sunday during that period.
I have a report, the user needs to select a date range, and from the date range it calculates each first-day-of-week and last-day of week and passes it in that way.
How it currently works in SSRS is
exec storedprocname
#BD=N'798211,798654,798664,798826',
#CGNo=N'47',
#SCGNo=N'4701,4702,4703,4704,4705,4706,4707,4708',
#ProductClass=N'1,2,4,3',
#ProductCode=N'1020',
#Region=N'772',
#FirstDayOfWeek='2014-01-06 00:00:00',
#LastDayOfWeek='2014-01-12 00:00:00'
User selects multiple Mondays and Sundays, the report is a matrix table and matrix's on first day of week
FirstdayOfWeek = '2014/06/09,2014/06/16'
LastdayOfWeek = '2014/06/15,2014/06/23'
What I need is a date range the user selects this and it will still pass it in the same way
#startdate '2015/01/01' = Thursday (for this select current week's Monday)
#startdate '2015/02/01' = Sunday
You can use SSRS inbuilt function WEEKDAYNAME
=WEEKDAYNAME(DATEPART("dw", Fields!myDate.Value)
OR
=WEEKDAYNAME(DATEPART("w", Fields!myDate.Value)
You can use weekdayname function to filter your dataset or matrix or use in Iff expression.
If you want to handle it in SQL Server you can use dataname function.

access query that returns records for previous year up to same day, same month as today but for previous year

I need help specifying an access criteria on a date field that would pull records from my database from the beginning of last year, 1/1/2014 to a date that has the same day, and same month as today. The reason for this information is to be able to able to compare year-to-date records(and later counts) for this year to year-to-date's count for last year... thus, if today's date is 8/20/2015, I would want to be able to pull from 1/1/2015 to 8/20/2015 and then compare it to 1/1/2014 to 8/20/2014.
Just for heads-up, I am using the same query and form to count records based on weekly, quarterly date-ranges, and so I cannot use textboxes with "Start" and "End" dates. Also, I cannot pre-specify any date in my query. Any idea will be greatly appreciated. Thank you all.
To get last year's year-to-date DateSerial will do what you want.
Where [DateColumn] >= DateSerial(year(now)-1,1,1)
and [DateColumn] <= DateSerial(year(now)-1,month(now),day(now))
Another option
Where [DateColumn] >= dateadd("yyyy", datediff("yyyy", 0, now)-2, 2 )
and [DateColumn <= DateAdd("yyyy",-1, now)
You need to use Date() in SQL:
Where [DateColumn] >= DateSerial(Year(Date())-1,1,1)
And [DateColumn] <= DateAdd("yyyy",-1,Date())
The following expression can be used as the criteria for the date field in the query designer
>="01/01/" & (Year(Date())-1) AND <=Day(Date()) & "/" & Month(Date()) & "/" & Year(Date())-1
Warning: using strings to build dates should be avoided when possible. DateSerial() is a better approach, but this will work in MS Access (Jet/ACE).

How to filter the current week as a range of dates

Hi I have a MySQL database which on I am setting up a table for a Study Calendar, fields are as follows:
SELECT
studycalendarpk,
studytopic,
`module`,
startdate,
enddate,
syllabusoutline
FROM studycalendar
What I am trying to do is to create a query so that for a dashboard php page it has a query that dispays the current weeks study programme. Can someone please tell me how to setup query to filter it so that it is selected if the current date is between the startdate and enddate, thank you
You have a startdate and an enddate for each row in your table, and if I understand your requirement correctly, you want to display all rows that meet these criteria.
WHERE enddate >= start of week
AND startdate < start of next week
You already have startdate and enddate in your table. This answer assumes that each row's enddate is constrained to be greater than or equal to the starttdate. If it isn't you'll get strange results.
You need a MySQL expression to compute the first day of the present week. Here's how you do that.
FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7))
This expression yields the Sunday immediately preceding CURDATE(). If your weeks are considered to start on Monday, use this instead (notice the -2).
FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -2, 7))
These are very useful expressions because they yield actual dates. Those dates can then be manipulated by date arithmetic such as somedate + INTERVAL 7 DAY which conveniently gives you the date a week later. This sort of arithmetic even works for the last week, and the first week, of a calendar year.
Putting it all together, here's what you do to select the records you want.
WHERE enddate >= FROM_DAYS(TO_DAYS(CURDATE())-MOD(TO_DAYS(CURDATE())-1,7))
AND startdate < FROM_DAYS(TO_DAYS(CURDATE())-MOD(TO_DAYS(CURDATE())-1,7))
+ INTERVAL 7 DAY
This will get the records from your table relevant to the current week.
SELECT *
FROM studycalendar
where curdate() between startdate and enddate
Can you try it? We can gett week no of use by this week() method
SELECT
`studycalendarpk`,
`studytopic`,
`module`,
`startdate`,
`enddate`,
CAST(week(now()) AS UNSIGNED)
syllabusoutline
FROM studycalendar WHERE CAST(week(now()) AS UNSIGNED) between CAST(week('2014-09-01') AS UNSIGNED) and CAST(week('2014-09-07') AS UNSIGNED)

ms access annual criteria

I need to create a query criteria to get any date from the last 1 may of last year till today, example if I run the query now it should get the data from 1 may 2012 till today, if I run the query the next year on Feb 2013 then get the data from 1 May 2012 till feb 2013.
update
I have used the below as the [JOINED DATE] query criteria but it returns nothing, what is wrong with this?
IIf(Month([Data]![JOINED DATE])>=5,Between DateSerial(Year(Now()),5,1) And Now(),Between DateSerial(Year(Now())-1,5,1) And Now())
Your syntax is incorrect, I do not advise including the "between" keyword in the IIF statement, you want your IIF to only return the date, something like:
SELECT *
FROM A
WHERE A.Date BETWEEN IIf(Month([Joined Date])>=5,DateSerial(Year(Date()),5,1),DateSerial(Year(Date())-1,5,1) AND Date()
Note: I have used Date() rather than Now() as Now() includes the timestamp which is not necessary in this case.