19-07-22 11:44 AM
Hi, BOT is processing approx. 5 excel files per day. Each file have a column with date field. Date field is not consistent in all the files. Possibility: File 1: Date format - mm/dd/yyyy File 2: Date format - dd/mm/yy File 3: Date format - DD-MM-YYYY File 4: Date format - mm-dd-yy. We are using "FormatDateTime (Date, DateFormat)" to get date in one unique format. But since date field format varies for each file, BOT may produce wrong result or may throw an exception. Is there a way BOT can sense date format from excel file and parse it into desired format without any error?
Thanks.
19-07-22 01:00 PM
20-07-22 04:01 AM
Then use any of the actions to pull the data from Excel into a Blue Prism collection. In this example, we had formatted the dates as "MMM d, yyyy".
21-07-22 01:23 PM
Thanks. The approach you suggested works great if the date field in the excel is in Date format rather if the field is text format how can I proceed that.
------------------------------
Shomesh
------------------------------
22-07-22 04:06 PM
22-07-22 08:05 PM
Microsoft.ACE.OLEDB.12.0;Extended Properties="Excel 12.0;HDR=YES;READONLY=TRUE"
Version numbers will vary depending on the version of the ACE driver you have and the version of Excel
Date Format |
Regex Pattern |
---|---|
MM/dd/yyyy |
"\d{2}/\d{2}/\d{4}$" |
dd/MM/yy |
"\d{2}/\d{2}/\d{2}$" |
dd-MM-yyyy |
"\d{2}-\d{2}-\d{4}$" |
MM-dd-yy |
"\d{2}-\d{2}-\d{2}$" |
(F) The testing for regular expression matches (E) always returns a True or False into the "Matched?" data item.
(G) The "Format Match?" decisions branch off depending whether the current value of the "Matched?" data item (F) is True or False.
(H) An exception stage is thrown in for good measure if none of the format's match.
(I) I'm going to assume, by your question, that your standard date format does not start with the year first. Bearing that in mind, two of the four formats can be directly converted into the system's date format and the other two cannot. In my case having the day first will through off direct date conversion so for those date formats, I want to create an expression to place into my next SQL query to reformat the string so that it can convert directly. For this, I can use the "Date Reformat" calculation stage to format a string as an ACE SQL expression:
The expression string will take the middle two numbers, add a "/", add the first two numbers, add a "/" and then add the last two number of the year.
"MID([Column Date],4,2)+""/""+LEFT([Column Date],2)+""/""+RIGHT([Column Date],2)"
(J) If you are looking for the (J) step in the process image, there isn't one. I missed it when inserting the letters and I'm not going to go back and change the process image.
(K) Since the second set of date formats can be directly converted into a date, I use the "Date Pass-Thru" calculation stage to create a string with the name of the date column in the Excel:
(L) The "Reformat Date Formula" data item now holds either an expression for switching the numbers around in the Excel date column or just the column name in the Excel date column.
(M) Here is where the magic starts to happen. I've set up a data item that contains a SQL query that will retrieve all the columns in the worksheet "Data" in the Excel but also has a formula and placeholder for the date column to convert it into a date and then format it in any date format we want (in this case "MMM d, yyyy"):
SELECT [Column 1]
, [Column 2]
, FORMAT(CDATE(@DateReformat),"MMM d, yyyy") AS [Column Date]
, [Column 4]
FROM [Data$]
This takes whatever is put in place of the place holder "@DateReformat", converts it to a date using the function "CDATE" and then take that date and format it as "MMM d, yyyy"
(N) Here is where the magic ends. Using the "Get Collection" action from the "Data - OLEDB" VBO again, we do a quick replace of the "@DateReformat" placeholder we have in the "SQL Query" data item (M) with the string we have in the "Reformat Date Formula" data item (L) before we pass it into the "SQL" parameter:
(O) What we end up with is all the data in the Excel along with reformatted date data being brought directly into your Blue Prism collection: