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),"-",""))
I have a scenario where i need to move files from one folder to different folders based on filename.
Example : In the main folder i have files with different names, All Files which contain specific name in the file name should go into different folders.
i created a expression task and File system task to achieve the same, but all the files in main folder are getting deleted but are not available in respective folders.
This is the expression i am using in expression task based on which i a setting the destination folder.
!(ISNULL( FINDSTRING( #[User::FileName] , #[User::FileV2] , 1 ) )) ? #[User::DestinationFolder] == #[User::Folder1] : !(ISNULL( FINDSTRING( #[User::FileName] , #[User::FileV3] , 1 ) ) )? #[User::DestinationFolder] == #[User::Folder2] :!(ISNULL( FINDSTRING( #[User::FileName] , #[User::FilePlacement] , 1 ) )) ? #[User::DestinationFolder] == #[User::Folder3] : #[User::DestinationFolder] == #[User::Folder3]
Let me know how this can done.
Findstring returns NULL only if search string or expression are null, in the case you are showing most likely returns 0 when not found and therefore ISNULL will always returns FALSE.
I suggest to use >0 to test the conditions, something like:
FINDSTRING( #[User::FileName] , #[User::FileV2] , 1 ) > 0 ...
In the image I have filepath and parent_path columns, The requirement is if my string is contains in filepath then replace with new string and in parent path too.
Example : E:\Images\Apple_icon.jpg search for "Images" if contains replace with "MyImages", like this=> E:\MyImages\Apple_icon.jpg
update tableName set FILEPATH=REPLACE(FILEPATH,'E:\Images\','E:\MyImages\') ,
PARENT_PATH=REPLACE(PARENT_PATH,'E:\Images','E:\MyImages')
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.
I want to change image name in table like below.
image name : test.png
replace with : test_E.png
I want _E at end of all image name in table using mysql query.
Use replace function
update <table>
set image=replace(image,'.png','_E.png')
you could use this, if the image extension is not same in the table
update <table>
set image=concat(substring(image,1,locate('.',image)-1),'_E',
substring(image,locate('.',image),lenght(image)))
You can use string functions of MySQL query:
UPDATE TABLE SET IMAGE_NAME = CONCAT(SUBSTR(IMAGE_NAME,(CHAR_LENGTH(IMAGE_NAME) - 4)),
'_E' , SUBSTR(IMAGE_NAME, -4)) WHERE ID = <put record id>;
SUBSTR(IMAGE_NAME,(CHAR_LENGTH(IMAGE_NAME)-4)) would return name of file - assuming extension is of 3 chars. For 'test.png' above function would remove '.png' and function would return 'test'
SUBSTR(IMAGE_NAME, -4) would return last four chars of string - so 'test.png' would return '.png'
using concat you can concat 'test', '_E' and '.png' - returning 'test_E.png'
Please refer to string functions reference of MySQL for further use
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html