crystal reports - how to extract a date from string

China☆狼群 提交于 2020-01-06 08:06:41

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/11758273/crystal-reports-how-to-extract-a-date-from-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!