Datediff in Crystal Report - crystal-reports

Before I start, I want to say sorry my question might be little silly since I'm a newbie for CR.
I put a datediff formula in Crystal Report ver 14.0.12,
and it returns incorrect result for some special cases.
The formula is like below;
DATEDIFF('M',{START_DATE},{END_DATE})
If the start date is 2018-05-01 and the end date is 2020-04-30, the result should be like '24',
but it returns '23'.
It seems if the date range is in the first or final day, it has above error.
In addition, I have another issue with other formula.
I put below formula in order to get 'next date' of certain date field,
DATE(YEAR({date_field}),MONTH({date_field}),DAY({date_field}+1))
and it has an issue when the date field is 'end date' of certain month.
For example, if the date field is 2020-03-31, expected result is 2020-04-01,
but my formula returns like '2020-03-01'.
Please let me know what should I do in order to get correct result.
Thanks a lot:)

In regards to your DateDiff() function.
Did you possibly make a typo when asking your question? The first argument for the function is the intervalType and is a String expression, therefore it should be contained within double quotes; you have single quotes in your question. Using single quotes should throw a syntax error. Also, the intervalType for Month should be expressed as a lowercase "m".
As for your issue with adding 1 day to a date, I would recommend using the DateAdd(intervalType, nIntervals, startDateTime) function to complete this task. Try this formula instead:
DateAdd("d", 1, {date_field})
However, be aware that this function will return a DateTime value, so if you want to remove the timestamp from the date that is returned, you will want to cast the entire function as a Date datatype using the Date() function as follows:
Date(DateAdd("d", 1, {date_field}))

Single quote is fine. Source of the problem is that adding 1 to 31 is 32 (which gets wrapped back to 1 due to date logic).
Suggestion above to use DateAdd() is the correct solution.

Related

Difference between previous and current month

I'm trying to calculate the difference between current month and previous month.
The date field in my DB called C_DATE (dd/MM/YYYY), and I'm using in my sheet a date filter in the format: Year(C_DATE)&'-'& Num( Month(C_DATE),'00'). So when the user choose month (for example 2022-05), he will see the difference in the data between May and April.
I've already tried to calculate by:
count ({<Answer_num={">=6"}, C_DATE={">=$(=(MonthStart(Max(C_DATE))))<=$(=(MonthEnd(Max(C_DATE))))"}>}Answer_num) -
count ({<Answer_num={">=6"}, C_DATE={">=$(=(AddMonths( MonthStart(Max(C_DATE)),-1)))<=$(=(MonthStart(Max(C_DATE)))))"}>}Answer_num)
but i'm getting wrong outcome. is it maybe because of the date filter format? What can I do?
Thank you!
I think this is a matter of getting the date format correct and using the proper set operator. This may work:
=Count({1<Answer_num={">=6"}, C_DATE={">=$(=Date(MonthStart(Max(C_DATE)), 'DD/MM/YYYY'))<=$(=Date(MonthEnd(Max(C_DATE)), 'DD/MM/YYYY'))"}>*$} Answer_num)
-
Count({1<Answer_num={">=6"}, C_DATE={">=$(=Date(AddMonths(MonthStart(Max(C_DATE)),-1), 'DD/MM/YYYY'))<=$(=Date(MonthStart(Max(C_DATE)), 'DD/MM/YYYY'))"}>*$} Answer_num)
This adds the =Date(..., 'DD/MM/YYYY') expression to the set expressions so that the formatting matches the format of the [C_DATE] field. We also add the * set operator so that we get the intersection between the selection ($) and our ">=...<=..." set expression.
I'm sort of guess on the use of the intersection operator, it could be that the union operator (+) is the correct one to use in this case.

PostgreSQL TO_DATE date/time field value out of range

I am using TO_DATE in one of my PostgreSQL functions and it is throwing errors like date/time field value out of range: "2021901". This is happening for the months of January to September as I need to add zeros in front of them. So I tried to execute a simple select query there as follows as I am using the same syntax in function.
SELECT TO_DATE(2021::varchar||09::varchar||'01','YYYYMMDD')
This is also giving me the error
ERROR: date/time field value out of range: "2021901"
SQL state: 22008
Now if I change the month to October, November, or December it works fine, but for all the other months, it is showing this error. I am actually new to Postgres and not sure how to fix this. It would be very much helpful if someone can point me in the right direction. Thanks
If your input values are numbers (integer), another alternative is to use make_date()
make_date(2021,9,1)
A better and easier way would be to just provide the date correctly and use TO_DATE, so as example do this:
SELECT TO_DATE('2021-09-01', 'YYYY-MM-DD')
If you really want to go your way, you can force a leading zero by using TO_CHAR, like this:
SELECT TO_DATE(2021::varchar||TO_CHAR(09, 'fm00')||'01','YYYYMMDD')
But I recommend to take the first propose.

Crystal Reports default value for parameter

I'm attempting to make the parameters for a Crystal Report more user-friendly for a client, who has requested that they be able to have the default values for a Start and End date parameter be the first and last day of the previous month.
I know how to use either a formula in CR or a stored procedure to produce these values, but I want to know if a variable can be used in the 'Default Value' setting for a parameter, or if it only allows for static entries. Does anyone know? Right now the user can set the date parameters to null and the stored procedure generates the data for the previous month on its own, but I thought it'd be nice if the date parameters actually displayed the dates that were being used as defaults. Thanks in advance!
You can do it, Try below process:
Create a parameter ?date with String datatype and take static and write two default strings as below:
First day of previous month
Last day of Previous month
Now go to record selection formula and write below code:
if ({?date}="First day of previous month") then
table.date=DateSerial(year(currentdate),Month(Currentdate)-1,1)
else if ({?date}="Last day of previous month")
then
table.date=Cdate(DateAdd("d",-1,DateSerial(year(currentdate),Month(Currentdate),1)))

Calculating days between today and a date field in an Access table

Hi I'm trying to calculate the number of days between a date field in my current access table and todays date. Here is what I have:
DAYS ON REPORT: DateDiff("d",[REPORTDATE],[DATE()])
The problem I am having is when I run the query I get a pop up box asking for "Date()"
I am trying to avoid adding a column for "today" and then doing the days between that column and the reportdate column if possible.
REPORTDATE is a column already existing in my table.
DAYS ON REPORT is the field/column I am creating.
When not enclosed in square brackets, Date() is a function call that returns the current system date. Putting square brackets around it turns it into a field name. Since there is no field with that name, the query treats it as a parameter and prompts for its value.
Solution: Get rid of the square brackets.

Number of days between past date and current date in Google spreadsheet

I want to calculate the number of days passed between past date and a current date. My past date is in the format dd/mm/yyyy format. I have used below mentioned formulas but giving the proper output.
=DAYS360(A2,TODAY())
=MINUS(D2,TODAY())
In the above formula A2 = 4/12/2012 (dd/mm/yyyy) and I am not sure whether TODAY returns in dd/mm/yyyy format or not. I have tried using 123 button on the tool bar, but no luck.
The following seemed to work well for me:
=DATEDIF(B2, Today(), "D")
DAYS360 does not calculate what you want, i.e. the number of days passed between the two dates – see the end of this post for details.
MINUS() should work fine, just not how you tried but the other way round:
=MINUS(TODAY(),D2)
You may also use simple subtraction (-):
=TODAY()-D2
I made an updated copy of #DrCord’s sample spreadsheet to illustrate this.
Are you SURE you want DAYS360? That is a specialized function used in the
financial sector to simplify calculations for bonds. It assumes a 360 day
year, with 12 months of 30 days each. If you really want actual days, you'll
lose 6 days each year.
[source]
Since this is the top Google answer for this, and it was way easier than I expected, here is the simple answer. Just subtract date1 from date2.
If this is your spreadsheet dates
A B
1 10/11/2017 12/1/2017
=(B1)-(A1)
results in 51, which is the number of days between a past date and a current date in Google spreadsheet
As long as it is a date format Google Sheets recognizes, you can directly subtract them and it will be correct.
To do it for a current date, just use the =TODAY() function.
=TODAY()-A1
While today works great, you can't use a date directly in the formula, you should referencing a cell that contains a date.
=(12/1/2017)-(10/1/2017) results in 0.0009915716411, not 61.
I used your idea, and found the difference and then just divided by 365 days. Worked a treat.
=MINUS(F2,TODAY())/365
Then I shifted my cell properties to not display decimals.
If you are using the two formulas at the same time, it will not work...
Here is a simple spreadsheet with it working:
https://docs.google.com/spreadsheet/ccc?key=0AiOy0YDBXjt4dDJSQWg1Qlp6TEw5SzNqZENGOWgwbGc
If you are still getting problems I would need to know what type of erroneous result you are getting.
Today() returns a numeric integer value: Returns the current computer system date. The value is updated when your document recalculates. TODAY is a function without arguments.
The following worked for me. Kindly note that TODAY() must NOT be the first argument in the function otherwise it will not work.
=DATEDIF( W2, TODAY(), "d")
Today() does return value in DATE format.
Select your "Days left field" and paste this formula in the field
=DAYS360(today(),C2)
Go to Format > Number > More formats >Custom number format and select the number with no decimal numbers.
I tested, it works, at least in new version of Sheets, March 2015.