abbreviations for sql server components when installing with powershell

Dynamic File Name

In a recent project we needed to create an SSIS package to export data from a SQL Server table to a CSV file. One of the requirements was to dynamically append the date to the file name each time a file was generated. This post will cover how to create a dynamic file name with the date included (YYYYMMDD format) in the file name.

You should already have created the output file in the connection managers. If you haven’t done that—complete that first.

How to create a dynamic file name

When it comes to creating a dynamic file name, the expression for the CSV must be updated. Right click the CSV in Connection Manager and click on Properties. This will open a section on the right hand side which contains an expressions row.

right click the csv in connection managerRight click the CSV in Connection Manager and click on Properties.

Clicking on the ellipses will open a section where you can create a new property connection.Clicking on the ellipses will open a section where you can create a new property connection.

The statement below can be copied and pasted into the expression box to create a path:The statement below can be copied and pasted into the expression box to create a path:

“\\\\Servername\\SHARE\\FileName_”+(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) +”.csv”

 

 

 

Note that the highlighted section will need to be updated to your environment—where are you putting the file created. The extra forward slashes in the file path are used to escape the other forward slashes for the file path. Adding one forward slash in the beginning of each forward slash (including one each for the two at the beginning of the file path) will ensure that your file path string gets interpreted correctly. The result will place a file name “FileName_YYYYMMDD” inside a folder called SHARE.

You might also move around parts of the expression if you want a different format.

Carlos Chacon
Director of Data Solutions

With more than 10 years of working with SQL Server, Carlos helps businesses ensure their SQL Server environments meet their users’ expectations. He can provide insights on performance, migrations, and disaster recovery. He is also active in the SQL Server community and regularly speaks at user group meetings and conferences. He helps support the free database monitoring tool found at databasehealth.com and provides training through SQL Trail events.

Let's Talk About Your Project.

We are a full-service IT and digital marketing firm. We believe that successful projects are the result of working collaboratively and transparently with our clients. Are you looking for a better user experience for your website or application? Need an experienced database architect or business analyst? Let’s talk!

Let's find what you're looking for