Link to home
Create AccountLog in
Avatar of rhopman
rhopman

asked on

Char to Date field in Crystal Reports

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?
Avatar of bdreed35
bdreed35
Flag of United States of America image

We can help you with a solution, but you will need to provide some sample data.
Avatar of rhopman
rhopman

ASKER

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
Avatar of Mike McCracken
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
Avatar of rhopman

ASKER

I get an error with the above code; specifically not enough parameters.
Try this:

Date({table.field}[5 to 6] & "/" & {table.field}[7 to 8] & "/" & {table.field}[1 to 4])
How are you entering the formulas?

WHere does it say the error is?

mlmcc
Avatar of rhopman

ASKER

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."
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

ASKER CERTIFIED SOLUTION
Avatar of bdreed35
bdreed35
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
You are correct for CR8 and in CR XI.

Help must not be complete.

mlmcc

Avatar of rhopman

ASKER

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