简体   繁体   中英

crystal reports - how to extract a date from string

Using Crystal Reports 2008, I need to extract a date from a text field. This date is usually in the format dd/mm/yy, but could also be entered as d/m/yy, dd/m/yyyy, etc.

This date could appear anywhere within the string.

At the moment I am relying on the fact that the date is placed at the end of the string, without a following fullstop, and using LEFT/RIGHT to extract each date part. These parts are then passed to another formula to create a full date:

Dim AllocationDate() as Date If Not(IsNull({Command.Notes})) then Formula = DateValue ((ToNumber ({@Year})), (ToNumber ({@Month})), (ToNumber ({@Day})))

However, if anyone uses a variation of format, adds a fullstop or more notes after the date the whole report keels over.

Is there any way I could extract this date by looking for a pattern? I'm guessing I could the use TRIM to get around the inconsistencies in format.

tyvmia

You may want to consider using a regular expression .

Crystal Reports doesn't have native support for regular expressions, so you'll need to add a UFL: crystal reports : is there a way to regex in crystal reports?

You should be able to adapt the pattern in this question for your needs: Javascript date regex DD/MM/YYYY

Finally, you can test the pattern on your text using regexpal.com .

** edit **

Create a SQL-expression field (Oracle 10 syntax) to extract date string and convert it to a date field:

// {%Allocation Date}
(
  -- match date-like string, then convert to date type; is no dates are found, NULL is returned
  TO_DATE( REGEXP_SUBSTR( TABLE.FIELD, '\d{1,2}\/\d{1,2}\/\d{2,4}',1 ,1), 'dd/mm/yyyy')
)

While you could

And, as a last resort, you can try converting the multi-line string into a long string by replacing the special characters that represent CR, LF, etc. Replacing them with spaces or another innocuous character, and then treat the resultant string as if it were just a regular string (with the date in the middle).

You would still have to make some assumptions to make this possible: ONE date per string, all special characters are known (or you have to test for all possible special characters), the date has SOME conformity to the format, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM