We help IT Professionals succeed at work.

Char to Date field in Crystal Reports

648 Views
Last Modified: 2012-06-27
I have two date fields that I need to make a calculation between. The problem is the date fields listed as a 8 char string, not a number. How do I tell Crystal Reports that make the 8 char field into a 8 string date field?
Comment
Watch Question

Top Expert 2004

Commented:
We can help you with a solution, but you will need to provide some sample data.

Author

Commented:
For instance:

Date1                    Date2
20040131             20040201

I have two fields that contain 8 characters in the form of YYYYMMDD. Unfortunately, the database administrator created the fields as chars, not date, int, etc...

I need to be able to format the two fields in Crystal to show as a date and then determine the time between date1 and date2, i.e. as in the example, 1 day.

Thanks
Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
Try using a formula like this to convert the date strings to dates.

Date(Val(Left({YourDateField}) , Val(mid({YourDateField},5,2)) , Val(Right({YourDateField},2))

You can use DateDiff('d',{@ConvDate1}, {@ConvDate2}) to get the number of days between 2 dates

mlmcc

Author

Commented:
I get an error with the above code; specifically not enough parameters.
Top Expert 2004

Commented:
Try this:

Date({table.field}[5 to 6] & "/" & {table.field}[7 to 8] & "/" & {table.field}[1 to 4])
Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
How are you entering the formulas?

WHere does it say the error is?

mlmcc

Author

Commented:
I created a formula field using crystal syntax. Using the following:

Date({table.field}[5 to 6] & "/" & {table.field}[7 to 8] & "/" & {table.field}[1 to 4])

I receive no error found when running a check on the formula, but when I refresh data, the error is "Bad Date Format String."
Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
TO use the string format you need (according to the help) to pass it as Dec 31, 1999

I see an error in my first try

Date(Val(Left({YourDateField}) , Val(mid({YourDateField},5,2)) , Val(Right({YourDateField},2))


should be

Date(Val(Left({YourDateField},4) , Val(mid({YourDateField},5,2)) , Val(Right({YourDateField},2))

mlmcc

Top Expert 2004
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
You are correct for CR8 and in CR XI.

Help must not be complete.

mlmcc

Author

Commented:
OK, I got this one to work fine for fields containing data.

local stringvar input := "20040201";
Date(input[5 to 6] & "/" & input[7 to 8] & "/" & input[1 to 4])

However, for the other date field, there are banks. What type of an if than statement do I need?

Thanks, Ryan
Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
What do you mean blanks?

do you mean it is NULL?

Try
What date do you want it to be when the field is blank?

If (IsNull({YourDateField}) OR {YourDateField} = "" OR {YourDateField} = "        ") then
    CurrentDate
Else
    Date(input[5 to 6] & "/" & input[7 to 8] & "/" & input[1 to 4])

mlmcc

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.