Using dateadd with specific conditions - crystal-reports

So I have two different date fields in my report. One is command.install date and the other is command.mfgdate. My problem is for some records mfgdate is blank and others installdate is blank and sometimes both dates exist in a record. I’m trying to use a formula with dateadd to return a replacement date based on a predetermined number of years. What I would like to do is use a formula that would first use mfgdate then use installdate as a “safety net” if mfgdate happens to be blank. Is there anyway that this is possible?

If a date shows "blank" inside the report, it usually means the date is NULL.
Therefore, the following formula should give you the desired result:
If Not IsNull({command.mfgdate}) Then
{command.mfgdate}
Else If Not IsNull({command.installdate}) Then
{command.installdate}
Else
// backup-date if both dates are NULL
CDate(1900,1,1)

Related

Crystal Reports: using date from the datatime field as a parameter

I am working on a report that I need to add a date range parameter. The field that I need to use in the datetime field, and I need to use the date portion from the field. I tried using the following, but the report returns no result.
cast(StartDateTime as date) between {?StartDate} and {?EndDate}
For the time being, I am using the Select Expert to sort the date range, but I have to manually enter the date in 'yyyy-mm-dd' format. Is there a way to set up the parameter, so that I can use the calendar to choose the dates?
I recommend to use one parameter field and set it to range and date instead of two.
With this you can use easily this formula:
{StartDateTime} = {?Parameter}
If you set the Parameter to Date instead of DateTime it will automatically check with the start of the day for the first range and the end of the day for the last range.

Datediff in Crystal Report

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.

The group options for a date, time or date-time condition field must be a date group

I'm using VS2017 and trying to convert my reports from Delphi to Asp.Net, but the problem with some until this point that My users can change their sort from the GUI and I need to dynamically adjust the sort in code to match their selection.
To do this I use the following code:
ReportDocument.DataDefinition.Groups[i].ConditionField = ReportDocument.Database.Tables[CrystalReportDatasource].Fields[cField];
However if cField is aDateField and the original is a StringField group I receive the following exception:
The group options for a date, time or date-time condition field must be a date
group options object crystal reports" when I try and excute the above
statement.
Any idea how to fix that?
When you group on a date, Crystal needs to know what type of date grouping you wish to apply (e.g. Every Day, week, or Month...).
You need to take care of that aspect in code or simply create a String formula to convert the date column to a string and Group on that formula instead of on the raw date column.

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

Tableau Using Parameters for a Date Selector

I've been looking into making a date selector so instead of filtering by Month, Week by adding filters then displaying the quick filters to correspond. I want to be able to dynamically change the date so I can choose between Quarter, Month and Week in a single drop down.
I have made the selector part no problem it shows but when I come to my calculated field, it's not behaving how I would like.
Here is my calculated field:
CASE [Parameters].[Date Select]
WHEN 'Quarter' THEN QUARTER([Date])
WHEN 'Month' THEN MONTH([Date])
WHEN 'Week' THEN WEEK([Date])
END
This gives 2 errors...
Unknown Function QUARTER called
Unknown Function WEEK called
I am puzzled because the month one is fine.
Normally in tableau to get the month/quarter/week, I click on the Date dimension and select Month or week and it filters and displays like so: MONTH(Date).
Can anyone tell me where I'm going wrong and how I can pull the week and quarter from date in the calculated field so my selector will work.
Thanks.
There are no QUARTER and WEEK functions in Tableau, as you can see in the image below.
What you should be using to get these values are:
DATEPART('quarter',[Date])
DATEPART('week',[Date])
For filtering the date using your parameter as filter, I'll assume that you want to filter the current quarter, month or week. In this case you'll need to setup your paramaters like this:
Then you'll create a Calculated Field with the following formula:
DATEPART([Date Selector],[Date]) = DATEPART([Date Selector],TODAY())
This should give you a True/False dimension which you will use as filter for the value True. After that you should have a working parameter filtering your data accordingly to the value set.