Sql Server SSIS package Flat File Destination file name pattern (date, time or similar)?

49,101

Solution 1

With the help of expressions you can make connection dynamic.

Select your flat file connection from Connection Managers pane. In Properties pane, click on Expression(...). Then choose ConnectionString Property from drop down list and in Expression(...) put your expression and evaluate it.

Expression build -

For day : (DT_STR,4,1252)DAY( DATEADD( "dd", -1, getdate() ))
For month: (DT_STR,4,1252)MONTH( DATEADD( "dd", -1, getdate() ))
For Year:  (DT_STR,4,1252)YEAR( DATEADD( "dd", -1, getdate() ))

Example expression(you need to tweak as per your requirement) -

"E:\\Backup\\EmployeeCount_"+(DT_STR,4,1252)DATEPART( "yyyy" , getdate() ) + RIGHT("0" + (DT_STR,4,1252)DATEPART( "mm" , getdate() ), 2) + RIGHT("0" + (DT_STR,4,1252)DATEPART( "dd" , getdate() ), 2) +".txt" 

which is giving E:\Backup\EmployeeCount_20140627.txt as value.

Please note - You need a working flat file connection so first create flat file connection whose connectionString property is then going to be replaced automatically by expression.

You can follow these step by step articles as well.

ssis dynamically naming destination

SSIS Dynamic Flat File Connection

enter image description here

enter image description here

Solution 2

Select your file connection in the Connection Managers, go to the Properties and click on the (...) beside expressions.

In the editor select ConnectionString from the Property column. In the Expression text box, you can enter something like "rootNameOfFile" + (DT_WSTR, 50)(DT_DBDATE)GETDATE() + ".csv"

Evaluate your expression to make sure you're getting what you expect, and voila!

Solution 3

If you don't have SSDT and thus can't edit this with a GUI here is how you edit the SSIS package directly:

Before:

<DTS:ConnectionManager
      DTS:refId="Package.ConnectionManagers[DestinationConnectionFlatFile]"
      DTS:ObjectName="DestinationConnectionFlatFile"
      DTS:DTSID="{C69365C4-EF12-4606-980B-E8862EE997A4}"
      DTS:CreationName="FLATFILE">
      <DTS:ObjectData>

After:

<DTS:ConnectionManager
      DTS:refId="Package.ConnectionManagers[DestinationConnectionFlatFile]"
      DTS:CreationName="FLATFILE"
      DTS:DTSID="{C69365C4-EF12-4606-980B-E8862EE997A4}"
      DTS:ObjectName="DestinationConnectionFlatFile">
      <DTS:PropertyExpression
        DTS:Name="ConnectionString">"C:\\Exportdir\\Filename_"
 + (DT_WSTR,4)DATEPART("yyyy",GetDate()) +
RIGHT("0" + (DT_WSTR,2)DATEPART("mm",GetDate()) ,2) +
RIGHT("0" + (DT_WSTR,2)DATEPART("dd",GetDate()),2) + "_" +
RIGHT("0" + (DT_WSTR,2)DATEPART("hh",GetDate()),2)+
RIGHT("0" + (DT_WSTR,2)DATEPART("mi",GetDate()),2) + ".csv"      
      </DTS:PropertyExpression>
        <DTS:ObjectData>
Share:
49,101
ʞᴉɯ
Author by

ʞᴉɯ

#SOreadytohelp

Updated on December 23, 2020

Comments

  • ʞᴉɯ
    ʞᴉɯ over 3 years

    I'm scheduling a SSIS package for exporting data to flat file.

    But i want to generate file names with some date information, such as foo_20140606.csv

    Is it possible?

    Thanks

  • Kritner
    Kritner almost 10 years
    Ooh, I like yours better, it has purdy pictures! :)
  • rinilnath
    rinilnath over 2 years
    Can you please tell how to make this parameterize when we deploy the package in remote server, I need to make the path configurable in the external server via config table.
  • Vikramsinh Shinde
    Vikramsinh Shinde over 2 years
    If you are using SSIS catalog as deployment model, then you have the option to configure Project/Package level parameters and connections. On SSMS, go to the Integration Services Catalog then just right click on Project/Package, you will see the option.