简体   繁体   English

水晶报表-如何从字符串中提取日期

[英]crystal reports - how to extract a date from string

Using Crystal Reports 2008, I need to extract a date from a text field. 使用Crystal Reports 2008,我需要从文本字段中提取日期。 This date is usually in the format dd/mm/yy, but could also be entered as d/m/yy, dd/m/yyyy, etc. 该日期通常采用dd / mm / yy格式,但也可以输入d / m / yy,dd / m / yyyy等。

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. 此刻,我依靠的事实是日期放置在字符串的末尾,而后没有句号,并使用LEFT / RIGHT提取每个日期部分。 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}))) 将Dim AllocationDate()设置为日期,如果不是(IsNull({Command.Notes})),则公式= 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. 我猜我可以使用TRIM来解决格式不一致的问题。

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? Crystal Reports不支持正则表达式,因此您需要添加一个UFL:Crystal Reports:是否可以在Crystal Reports中使用正则表达式

You should be able to adapt the pattern in this question for your needs: Javascript date regex DD/MM/YYYY 您应该能够根据需要调整此问题中的模式: Javascript日期正则表达式DD / MM / YYYY

Finally, you can test the pattern on your text using regexpal.com . 最后,您可以使用regexpal.com在文本上测试模式。

** edit ** **编辑**

Create a SQL-expression field (Oracle 10 syntax) to extract date string and convert it to a date field: 创建一个SQL表达式字段(Oracle 10语法)以提取日期字符串并将其转换为日期字段:

// {%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). 并且,作为最后的选择,您可以尝试通过替换表示CR,LF等的特殊字符,将多行字符串转换为长字符串。用空格或其他无害字符替换它们,然后将结果字符串视为它只是一个常规字符串(日期在中间)。

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. 您仍然需要做出一些假设才能使此操作成为可能:每个字符串一个日期,已知所有特殊字符(或者您必须测试所有可能的特殊字符),日期与格式具有某些一致性,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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