extract date from file name using SSIS expression - ssis

HT,
Using Microsoft SSIS.
I have a input CSV file called - LatLong_WD_Locations_06-21-2021.
I want to extract date from this file name in 20210621 format using SSIS expression and store in to a variable called v_FileDate which is Int32 type.variable v_FileName is a string type.
I tried with
(DT_I4) REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "_", 2)+1,10),"-","")
but its not working.
Can I have some help here, please.
TIA

Almost there. You specified 2 for the occurrence argument in FINDSTRING expression. So it is finding the _ before Location in the file name giving you a result of Locations_. Since that is not a integer it is throwing an error.
Change the 2 to a 3:
(DT_I4) REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "_", 3)+1,10),"-","")
The above would account for if the V_FileName has a file extension. It would not get you the final format of yyyyMMdd. See below...
You could also simplify and use RIGHT expression. Get the right 10 characters of the string and then replace:
(DT_I4) REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-","")
I updated the above statement to account for if v_FileName had an extension. That still does not give your final format of yyyyMMdd. See below...
Those 2 expressions above will get the date out of the v_FileName, but in format MMddyyyy. Now you will have to parse out each part of the date and put it back together using one of the above statements. The example below is using the one with RIGHT:
(DT_I4) SUBSTRING(REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-",""), 5,4)
+ SUBSTRING(REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-",""), 1,2)
+ SUBSTRING(REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-",""), 3,2)

If you ever have Date on the last 10 positions in file name solution is very simple. But if that is not case, write me below in a comment and I will write a new expression.
Solution explained step by step:
Get/create variable v_FileDate with value LatLong_WD_Locations_06-21-2021
For check create DateString with expression
RIGHT( #[User::v_FileDate], 4) +
LEFT (RIGHT( #[User::v_FileDate], 10), 2) +
LEFT (RIGHT( #[User::v_FileDate], 7), 2)
Create a final variable DateInt with expression
(DT_I4) #[User::DateS]
How variable should like:
Or you can with a single variable (It would be better with a single variable).
(DT_I4) (
RIGHT( #[User::v_FileDate], 4) +
LEFT (RIGHT( #[User::v_FileDate], 10), 2) +
LEFT (RIGHT( #[User::v_FileDate], 7), 2)
)
Final Result

Using script task...
Pass in v_FileDate as readwrite
Pass in v_FileName as readonly
Code:
//Get filename from SSIS
string fileName = Dts.Variables["v_FileName"].Value;
//Split based on "_"
string[] pieces = fileName.Split('_');
//Get the last piece [ref is 0 based and length is actual]
string lastPiece = pieces[piece.Length -1];
//Convert to date
DateTime d = DateTime.ParseExact(lastPiece, "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture);
//Convert to int from string converted date in your format
Dts.Variables["v_FileDate"].Value = int.Parse(d.ToString("yyyyMMdd"));

Some work around -Working for me in (YYYYMMDD) format.
Thanks #keithL
(DT_I4)( REPLACE(RIGHT(#[User::v_FileName], 5),"-","")+REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "", 3)+1,2),"-","")+REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "", 3)+3,4),"-",""))

Related

Find the position of the first occurrence of any number in string (if present) in MS Access

In MS Access I have a table with a Short Text field named txtPMTaskDesc in which some records contains numbers, and if they do, at different positions in the string. I would like to recover these numbers from the text string if possible for sorting purposes.
There are over 26000 records in the table, so I would rather handle it in a query over using VBA loops etc.
Sample Data
While the end goal is to recover the whole number, I was going to start with just identifying the position of the first numerical value in the string. I have tried a few things to no avail like:
InStr(1,[txtPMTaskDesc],"*[0-9]*")
Once I get that, I was going to use it as a part of a Mid() function to pull out it and the character next to it like below. (its a bit dodgy, but there is never more than a two-digit number in the text string)
IIf(InStr(1,[txtPMTaskDesc],"*[0-9]*")>0,Mid([txtPMTaskDesc],InStr(1,[txtPMTaskDesc],"*[0-9]*"),2)*1,0)
Any assistance appreciated.
If data is truly representative and number always preceded by "- No ", then expression in query can be like:
Val(Mid(txtPMTaskDesc, InStr(txtPMTaskDesc, "- No ") + 5))
If there is no match, a 0 will return, however, if field is null, the expression will error.
If string does not have consistent pattern (numbers always in same position or preceded by some distinct character combination that can be used to locate position), don't think can get what you want without VBA. Either loop through string or explore Regular Expressions aka RegEx. Set reference to Microsoft VBScript Regular Expressions x.x library.
Function GetNum(strS AS String)
Dim re As RegExp, Match As Object
Set re = New RegExp
re.Pattern = "[\d+]+"
Set Match = re.Execute(strS)
GetNum = Null
If Match.Count > 0 Then GetNum = Match(0)
End Function
Input of string "Fuel Injector - No 1 - R&I" returns 1.
Place function in a general module and call it from query.
SELECT table.*, GetNum(Nz(txtPMTaskDesc,"")) AS Num FROM table;
Function returns Null if there is no number match.
Well, does the number you want ALWAYS have a - No xxxx - format?
If yes, then you could have this global function in VBA like this:
Public Function GNUM(v As Variant) As Long
If IsNull(v) Then
GNUM = 0
Exit Function
End If
Dim vBuf As Variant
vBuf = Split(v, " - No ")
Dim strRes As String
If UBound(vBuf) > 0 Then
strRes = Split(vBuf(1), "-")(0)
GNUM = Trim(strRes)
Else
GNUM = 0
End If
End Function
Then your sql will be like this:
SELECT BLA, BLA, txtPMTaskDesc, GNUM([txtPMTaskDesc] AS TaskNum
FROM myTable
So you can create/have a public VBA function, and it can be used in the sql query.
It just a question if " - No -" is ALWAYS that format, then THEN the number follows this
So we have "space" "-" "space" "No" "space" "-" -- then the number and the " -"
How well this will work depends on how consistent this text is.

How to insert change only part of the value in a mysql table column

The column "path" has values that are represented in the following format:
http://helloworld.com/media/recordings/tape1.mp4
http://helloworld.com/media/recordings/tape2.mkv
http://helloworld.com/media/editing/tape2-a.mkv
how can I append a value to only file name part of the url? e.g - 20141010 (there will be only 1 date applied to all values)
http://helloworld.com/media/recordings/20141010tape1.mp4
http://helloworld.com/media/recordings/20141010tape2.mkv
http://helloworld.com/media/editing/20141010tape2-a.mkv
Can possibly use a CONCAT command but I'm not sure how to tell it to only remand entry after last /
UPDATE table
SET path = INSERT(path, LENGTH(path) - LOCATE('/', REVERSE(path)) + 2, 0, '201410110');
See String Functions for the documentation of all these functions that are used to process strings.

SSIS expression for date comparison?

My expression is like following:
DATEDIFF("dd",(DT_DATE)(SUBSTRING(#[User::strExcelFileName],15,2) + "-" + SUBSTRING(#[User::strExcelFileName],18,2) + "-" + SUBSTRING(#[User::strExcelFileName],21,4)),GETDATE()) > #[User::intDaysCount]
My file : TempConfigPr_06172013.xlsx
The problem is it's saying error while converting from "DT_WSTR" to data type "DT_DATE"...Could any one please help, where I'm going wrong and how to solve this?
Note: I checked,this expression is working fine.
DATEDIFF("dd",(DT_DATE)("11-18-2010"),GETDATE()) > #[User::intDaysCount]
Editing expressions in SSIS can be painful. I find when they aren't working as expected, it's best to break them into multiple variables and have them feed off each other.
By doing so, I quickly determined that your root problem was this expression was not generating a valid date value. It generated 61-20-3.xl
SUBSTRING(#[User::strExcelFileName],15,2) + "-"
+ SUBSTRING(#[User::strExcelFileName],18,2) + "-"
+ SUBSTRING(#[User::strExcelFileName],21,4)
I created a variable strExcelFileDate of type string and I used the expression to create a string that is the 8 characters following the underscore.
SUBSTRING(#[User::strExcelFileName], FINDSTRING(#[User::strExcelFileName], "_", 1)+1, 8)
That string value, 06172013, cannot be directly cast to a date, which is a pity. I needed to slice and dice that string into something that can be cast to a date data type. A new Variable named dtExcelFileDate with a type of DateTime was created. I used an expression on that to transform the string into yyyy-mm-dd and then cast that entire thing to a DT_DATE data type.
(DT_DATE) (RIGHT(#[User::strExcelFileDate], 4) + "-"
+ SUBSTRING(#[User::strExcelFileDate], 1,2)
+ "-" + RIGHT(SUBSTRING(#[User::strExcelFileDate],1,4), 2))

How to split this String in two parts?

I would like to split a string like this in Access 2000 (Visual Basic function):
"[Results]
[Comments]"
in two parts:
the results part
the comments part
As you can notice, these two parts are separated by an empty line (always, this is our separator).
[Results] and [Comments] are blocks of text. We don't care what's in it except:
the results part doesn't have any empty lines in it, so the first empty line we see is the separator one.
I want my function to extract the Comments part only.
Here is what i tried:
Public Function ExtractComm(txt As String) As String
Dim emptyLine As Integer
txt = Trim(txt)
'emptyLine = first empty line index ??
emptyLine = InStrRev(txt, (Chr(13) + Chr(10)) & (Chr(13) + Chr(10)))
'Comments part = all that is after the empty line ??
ExtractComm = Mid(txt, emptyLine + 4)
End Function
But it doesn't work well.
If I do:
ExtractComm(
"Res1
Res2
Comment1
Comment2"
)
I want to obtain:
"Comment1
Comment2"
but I only obtain Comment2. Any idea to extract the comment part ?
Thanks a lot !
Maybe you need to use InStr instead of InStrRev
InStrRev
Returns the position of the first occurrence of one string within another, starting from the right side of the string.
InStr
Returns an integer specifying the start position of the first occurrence of one string within another.

Append Input File Name to date based on condition in SSIS Variable Expression

I have couple of CSv input files with different filenames along with difeerent date formats. i need to select the input file dynamically based on a condition .
For example
i have files file120120909.csv, scalefile09102012.csv
i need to retrive the input file based on condition. So i have taken an user variable and return expression in this way
(#[User::Type] =="File" ? "file" : (#[User::type] =="scale" ? "scale_" :"NA")) + (DT_STR,4,1252)DATEPART("yyyy", #[System::StartTime] ) + RIGHT("0" + (DT_STR,2,1252) DATEPART("mm", #[System::StartTime]),2) + RIGHT("0" + (DT_STR,2,1252)DATEPART("dd", #[System::StartTime]),2)
when i evaluate the expression i got the following result
file120120912
now when i assign value for user variable #[User::Type] =="scale" then the expression result has to be scalefile09102012.
So based on condition how can i append the date for the file
You can write the VB.NET code into the script task to solve your issue:
Dim type As String = Dts.Variables("Type").Value.ToString()
Dim startDate As DateTime = DateTime.Parse(Dts.Variables("StartTime").Value.ToString())
Dim result As String = ""
If type.ToLower() = "file" Then
result = type.ToLower() + startDate.ToString("yyyyMMdd")
ElseIf type.ToLower() = "scale" Then
result = type.ToLower() + "file" + startDate.ToString("MMddyyyy")
Else
result = "incorrect type"
End If
MsgBox(result)