• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

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?
0
rhopman
Asked:
rhopman
  • 5
  • 4
  • 3
1 Solution
 
bdreed35Commented:
We can help you with a solution, but you will need to provide some sample data.
0
 
rhopmanAuthor 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
0
 
mlmccCommented:
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
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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

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

WHere does it say the error is?

mlmcc
0
 
rhopmanAuthor 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."
0
 
mlmccCommented:
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

0
 
bdreed35Commented:
The formula I provided should work just fine.
I just tested this out just to be sure:

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

This returns 2/1/2004 in a date datatype.
Is your data always 8 characters?  That would certainly effect how the formula works.
0
 
mlmccCommented:
You are correct for CR8 and in CR XI.

Help must not be complete.

mlmcc

0
 
rhopmanAuthor 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
0
 
mlmccCommented:
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now