Wednesday, June 10, 2015

Using a Date Stamped File as a File Connection DataSource in SSIS

Quick Description:  How to set up an SSIS job to look for a file stamped with today's (or another day's) date. Also covering the difference between leading 0s/two digit day and month in SSIS expression date strings.

How To:  Create a flat file connection, initially hardcode the location of a sample file so that you can set datatypes, set the file connection's 'Connection String' property to a variable, populate the variable with an expression that pulls date parts out of the date you're looking for.


  • In a Data Flow task, create a Flat File Connection by dragging a Flat File Source into the Connection Manager pane.
  • Create a variable to hold the file name.  Right click the surface of the Data Flow task, and select Variables.
  • Open the Flat File connection, and in the File Name box, enter the name of a sample file and perform general setup of fields and datatypes
  • Create a variable of type string.
  • Close the dialog box, highlight the connection manager for the file in question, and go to the properties pane.
  • In the properties pane, select Expressions.
  • Select your variable and close the dialog.
  • To populate the variable with the correct file name, break down which date parts are in what order (year, month, day, hour, minute, second) and create the expression using the date parts as below.
  • Create a variable to hold the file name.  Right click the surface of the Data Flow task, and select Variables.

Below are examples for leading 0 and no leading 0 (always two digits regardless of whether the number is 10, vs. only one digit when under 10)

Assuming today's date is 2015-06-10 12:39
If you want the leading 0
"D:\\myFilePath\\" +
Right("0" + (DT_STR,4,1252) DatePart("yyyy",getdate()),4) +
Right("0" + (DT_STR,4,1252) DatePart("mm",getdate()),2) +
Right("0" + (DT_STR,4,1252) DatePart("dd",getdate()),2) +
Right("0" + (DT_STR,4,1252) DatePart("hh",getdate()),2) +
Right("0" + (DT_STR,4,1252) DatePart("mi",getdate()),2) +
"_MyFileName.txt"
Evaluates to:   D:\myFilePath\201506101239_MyFileName.txt


If you don't want leading 0s
"D:\\myFilePath\\" +
(DT_WSTR, 4)(DATEPART("yyyy", GETDATE())) +
(DT_WSTR, 4)(DATEPART("mm", GETDATE()))+
(DT_WSTR, 4)(DATEPART("dd", GETDATE()))+
(DT_WSTR, 4)(DATEPART("hh", GETDATE())) +
(DT_WSTR, 4)(DATEPART("mi", GETDATE()))+
 "_MyFileName.txt"
This evaluates to:  D:\myFilePath\20156101239_MyFileName.txt

No comments:

Post a Comment