crystal reports - how to extract a date from string - crystal-reports

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.

Related

extracting date from from string

I have following string:
CO_CAS_REA_NO_VIPPLUS_20190402_BONUS
I would like to Extract the Date part (20190402) and convert it into DD/MM/YY format if possible.
I have tried some 'Search' and 'extraction' formulas but to no avail.
does anyone have any ideas how to do this in excel or VBA?
Other variations of the data string would be like:
COCASREAHVPLUSVIPUK20190827CRMFSDEP
COCASREALVWINFIDEGL20190809CRMPTS
COSBINFLVFIDE20190830CRMBET
CO_CAS_RET_HVMV_UK_20190830_RB
COSB_REA_181INF_HVMV_FIDE_20190809_CRM_RB
COSBREAHVMVGL20190831CRMFBFSQUAL
COSBINFLVNO20190816CRMFB
Suppose in each string you only have one 8-digit numerical string representing the date in the form of YYYYMMDD that you want to extract, you can use one of the following three formulas to return the date:
Method 1:
=DATE(LEFT(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),4),MID(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),5,2),RIGHT(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),2))
Method 2:
=DATEVALUE(LEFT(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),4)&"/"&MID(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),5,2)&"/"&RIGHT(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),2))
Method 3:
=DATEVALUE(TEXT(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),"0000-00-00"))
Please note, all methods are using array formula so you MUST press Ctrl+Shift+Enter upon finishing the formula in the formula bar otherwise they will not function correctly. Then you can simply drag the formula down to apply across.
The logic is to use this array formula =MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)) to extract the numerical string first. Method 1 & 2 follow the same logic which is to extract the year YYYY, month MM and day DD component from the numerical string and then use use DATE or DATEVALUE function to return the date. Method 3 is using a formatting trick to quickly turn the numerical value into a text date and then use DATEVALUE function to convert the text date into a "real" date.
Let me know if you have any questions. Cheers :)
Assume your data put in Column A
In B2, enter formula :
=0+TEXT(AGGREGATE(14,6,--MID(A1,ROW($1:$250),8),1),"0000-00-00")
Then, B2 >> Format Cell, in the Number tab, choose: "Date" >> select dd/mm/yy
All copied down
with excel-formula, make use of MID() function
=MID("CO_CAS_REA_NO_VIPPLUS_20190402_BONUS",23, 8)
with VBA, you can split it with "_"
dte_str = Split("CO_CAS_REA_NO_VIPPLUS_20190402_BONUS", "_")(5)
You might find this more flexible if the text to the left changes:
Just to save you typing here is the formula in cell C1:
=MID(A1,FIND("LUS_",A1,1)+4,FIND("_BON",A1,1)-FIND("LUS_",A1,1)-4)
It does however assume that the 4 characters either side of the date are always the same.
Assuming the strings are in Column A and you're only looking for the year 2019 you could use this
=DATE(LEFT(MID(A1,SEARCH(2019,A1),8),4), MID(MID(A1,SEARCH(2019,A1),8),5,2), RIGHT(MID(A1,SEARCH(2019,A1),8),2))

Google Sheets - Concatenate NOW-1, slash, and NOW+120

In Google Sheets, I need to create a date in the following format:
2016-06-15T12:00-0800/2016-10-16T12:00-0800
(Yesterday's date / today's date + 120 days)
Using =NOW()-1, I get yesterday's date.
Using Format - Date - More - Year(1930)-Month(05)-Day(01)T:Hour(01):00-0800, I get the proper format for the 1st part of the date range (2016-06-15T12:00-0800).
Repeating the same process with =NOW()+120.
Got the 2nd part of the date range (2016-10-16T12:00-0800).
PROBLEM: Trying to =CONCATENATE(A2,"/",B2), results in this:
42901.6965777315/43022.6965777315
...and no matter what I do - change the format, try to use =CONCATENATE(=TEXT(A2),"/",=TEXT(B2)), or other tricks I know, I either get a blank cell, an error message, or an even worse mess.
All I want is to combine 2 date cells into 1, with a slash in between. How can this be accomplished?
Try join instead of concatenate:
=join("/",A1,B1)
Maybe:
=text(now()-1,"yyyy-mm-ddThh:mm")&"-0800/"&text(now()+120,"yyyy-mm-ddThh:mm")&"-0800"
TEXT with only a date as argument returns the serial number corresponding to that date,
First you should confer the result of you formulas that involves NOW to a formatted text by using TEXT with the second argument. Then you could concatenate the result of that.
The above be donde on a single formula but you maybe should start by doing each step on separate cells in order to make it easy to check the result of each part.

Convert text date format to number date format

I have stored some data entrys in a CSV file in following Format:
Thu Jul 28 08:42:33 GMT+01:00 2016
and need to convert it to just a time stamp (eg. h:m:s). How can I quickly and easily do this?
To convert a text date in a cell to a serial number, you use the DATEVALUE function. Then you copy the formula, select the cells that contain the text dates, and use Paste Special to apply a date format to them. Select a blank cell and verify that its number format is General.
If the size is fixed, then assuming the string is in A1:
=MID(A1,12,8)
The result can then be converted to an actual time value using TIMEVALUE.
Looks like a simple call to a single function would extract the substring you want, since it begins at a specific offset and only runs for eight characters (two hh plus colon plus two mm plus colon plus two ss).

Changing the Access date format

In an Access form, I need to use the date output from a date picker field to be used to search for a file in a form. However, the default data format seems to only allow slashes. Although I know the date is being stored as a number in the database.
Put simply, I need Me.myDate to output a legal file name structure, for example, yy-mm-dd. Then I can concatenate that with my file name to search for myfilename_yy-mm-dd
I always get myfilename_yy/mm/dd which is of course unusable as a file name.
Changing the format of the field obviously makes no difference to how the value is stored. What I don't understand is if the date is stored as a number, and the output is shown in the selected format, how I can override the default formatting behaviour used when I query the date.
The Format Function will allow you to format the date as you wish.
An expression to include the formatted date in a filename pattern could look like this ...
"myfilename_" & Format(Me.myDate, "yy-mm-dd") & "*"

Struggling with dates formats, want YYYY-MM-DD

As an absolute beginner to SAS I quickly ran into problems with date formatting.
I have a dataset containing transaction with three types of dates: BUSDATE, SPOTDATE, MATURITY. Each transaction is represented on two lines, and I want BUSDATE and SPOTDATE from line 1 but MATURITY from line 2.
In the original set, the dates are in YYYY-MM-DD format.
DATA masterdata;
SET sourcedata(rename(BUSDATE=BUSDATE2 SPOTDATE=SPOTDATE2 MATURITY=MATURITY2));
BUSDATE=BUSDATE2;
SPOTDATE=SPOTDATE2;
IF TRANS_TYPE='Swap' THEN;
MATURITY=SPOTDATE;
RUN;
Problem is, this returns something like 17169 (which I guess is the number of days from a certain date).
How can I make it output in YYYY-MM-DD format - or is this approach wrong; should I first convert the date variables to some SAS date format?
if you have valid SAS dates, just add a FORMAT statement to your DATA STEP.
Format busdate spotdate maturity yymmdd10. ;
SAS dates are numeric variables. They represent the number of days since 1/1/1960. You use a FORMAT to display dates.
Adding to CarolinaJay's answer, you normally want to keep them as numeric format, since you can do math (like "# of days since date X") with them. However, if for some reason you need a character variable, you can do this:
date_As_char=put(datevar,YYMMDD10.);
Incidentally, YYMMDD10 will actually give you YYYY-MM-DD, as you asked for; if you want a different separator, see http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000589916.htm (YYMMDDxw. format) - if you put a letter after the last D, for certain letters, you get a different separator. Like, YYMMDDn10. gives you no separator, or YYMMDDs10. gives you slashes. YYMMDDd10. gives you dashes, just like omitting the letter would. This concept also applies to MMDDYY formats, and I think a few others.