Substring with Findstring SSIS - ssis

can someone help me to correctly formulate the expression that I am using to extract a value within a string:
(2222) 010A07_TM
I need to extract only the expression 010A07, at the moment I am using the following, but it hasn't worked:
(DT_STR,50,1252)(SUBSTRING(FIELD_NAME,FINDSTRING(FIELD_NAME," ",1) + 1,FINDSTRING(FIELD_NAME,"_",1) - 2)))
PLIS

The third parameter of substring function is the string length. The second index must be subtracted from the first index.
FINDSTRING(FIELD_NAME,"_",1) - FINDSTRING(FIELD_NAME," ",1) -1
The final code will be as follows:
SUBSTRING(FIELD_NAME,FINDSTRING(FIELD_NAME," ",1) + 1,FINDSTRING(FIELD_NAME,"_",1) - FINDSTRING(FIELD_NAME," ",1) - 1)

Related

SSIS using derived column to convert data from mm/d/yyyy to dd/mm/yyyy

I have a SSIS package that inserts data from csv into SQL database.
This file has a data column that the format is different from SQL. I am getting error because the SSIS says that the data can not be convert.
The datacolumn is like this 11/9/2022 12:00:00 AM this data is related to 9th of november.
I need to use derived column to convert this data into dd/mm/yyyy.
I was trying using SUBTRING but I noticed that as firts character are the month we do not have the same number of character before the first / and the same for day.
I was wondering if I could try something like that
SUBSTRING([Dt],FINDSTRING("/",[Dt],1) + 1,FINDSTRING("/",[Dt],2) - 1) + "/" + LEFT([Dt],FINDSTRING("/",[Dt],1) - 1) + "/" + SUBSTRING([Dt],FINDSTRING("/",[Dt],2) + 1,4)
But I am getting the same error
How can I do to convert 11/9/2022 into this 09/11/2022 (dd/mm/yyyy) using derived column?
You were on the right path, but had the wrong syntax for FINDSTRING - the string to search comes first.
After a bit of trial and error I came up with this:
((FINDSTRING([Dt],"/",2) - FINDSTRING([Dt],"/",1)-1)==1?"0":"") + SUBSTRING ([Dt],FINDSTRING([Dt],"/",1)+1, FINDSTRING([Dt],"/",2) - FINDSTRING([Dt],"/",1)-1) + "/" + (FINDSTRING([Dt],"/",1)==2?"0":"") + SUBSTRING ([Dt],1,FINDSTRING([Dt],"/",1)-1) + "/" + SUBSTRING ([Dt],FINDSTRING([Dt],"/",2)+1,4)
SSIS functions can't be multi-line so the above is on 1 line, this is over multiples lines for ease of reading
((FINDSTRING([Dt],"/",2) - FINDSTRING([Dt],"/",1)-1)==1?"0":"") +
SUBSTRING ([Dt],FINDSTRING([Dt],"/",1)+1, FINDSTRING([Dt],"/",2) - FINDSTRING([Dt],"/",1)-1) + "/" +
(FINDSTRING([Dt],"/",1)==2?"0":"") +
SUBSTRING ([Dt],1,FINDSTRING([Dt],"/",1)-1) + "/" +
SUBSTRING ([Dt],FINDSTRING([Dt],"/",2)+1,4)
The 1st and 3rd lines are to handle if the day or month is single number, and adds a 0 of it is.
This stuff is always easier in c#. Assuming you have a string, then use script componentas follows:
var newCol = DateTime.ParseExact(row.ColumnName
, "MM/dd/yyyy hh:mm:ss tt"
, System.Globalization.CultureInfo.InvariantCulture)
.ToString("dd/MM/yyyy");
All that being said. It is not great practice to carry strings around that represent dates. Once properly loaded into a date, keep it as a date until the final presentation layer.

extract date from file name using SSIS expression

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),"-",""))

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))