SSIS Create extract file with Date and Time for a filename

In last weeks posting, we created our first SSIS package which queried a database and saved the information to a file so we could send the information to a user.  This week we are going to modify that same package to save the file with the date and time included in the filename.

Lets start by opening the project called etl_sample that we previously created and go to the Data Flow tab.  We are going to add a new item from the SSIS Toolbox called Multicast.  This allows us to split a process and go to two different processes.

 

et1

 

 

Once you drag it over, connect it between the Ole DB Source and the Flat File destination like below.

et2

 

 

Drag over another Flat File Destination from the Toolbox and attach it to the Multicast process.  This creates a new destination with a red circle which means we need to do additional setup within it.  I will not go through all of the tasks in setting up a new one since we did that last week, but you can reference it here.

et3

 

 

I did make the changes below on the destination config screen.  I called the file c:\temp\etl_sample_, and I made sure I selected the put column names in the first data row and press Ok.

et4

 

It then puts us back to the Data Flow tab, and we no longer have a red circle by our new flat file destination.

 

et5

 

Now we need to right click the Flat file Connection Manager 1 and click Properties.

 

et6

 

 

On the bottom right where the properties are located, scroll down to the expressions property and click the ellipses on the right.

et7

 

This will bring up the Expressions Editor.  Select ConnectionString from the property box, and then click the ellipses in the Expression box.

et8

 

We now need to create an expression to pass through to create the filename in a format that we desire.  Basically we are concatenating multiple parts of a date separated by an underscore to create the filename.  You can find more information about DATEPART right here, https://www.w3schools.com/Sql/func_datepart.asp.

If you click Evaluate Expression at the bottom of the screen, you will know right away if you have the format correct.  When I pressed the button, it shows the filename as c:\temp\etl_sample_02192017_14_22_16.csv.

Etl_sample_ we created on the file destination screen. 021920017 is for February 19, 2017. And the 14 is for 2pm, 22 is for minutes and 16 is for seconds:  14:22:16.

Below is the code so you do not have to type it in.  Feel free to experiment with other functions.  We could have made the extension DAT like the other file, but I called this one CSV to show up easier in the screenshots below.  Click Ok and OK again.

  “C:\\temp\\etl_sample_” + right(“0” + (DT_STR,4,1252) DatePart(“m”,getdate()),2)
+ right(“0” +(DT_STR,4,1252) DatePart(“dd”,getdate()),2)
+ (Dt_STR,4,1252) DatePart(“yyyy”, getdate()) +”_” + (dt_str,2,1252) datepart(“hh”,getdate()) +”_”
+ (dt_str,2,1252) Datepart(“mi”,getdate()) + “_”
+ (dt_str,2,1252) Datepart(“ss”,getdate()) + “.csv”

 

et9

 

Lets run our project and see if it works.  All green checkmarks, all good!

et10

 

 

If we look in the destination directory, we see two files got created, etl_sample.dat and etl_sample_02192017_14_23_46.csv.

et11

 

Opening up the files in Notepad, we see they have the same information in them.

 

et12

 

 

Lets run the SSIS package again and see what happens in the directory.  The file with the name etl_sample.dat got overwritten since the name is hardcoded in our first flat file destination which may be bad if we need to go back and see what was in the file on a certain date.  Underneath that, we can see there are now two files with names that include the date and time it was run.

et13

 

We just learned to create a flat file destination with a dynamic name that includes the date and time.  This way you can run your processes multiple times in a day without overwriting previous runs, or you can use it as proof that your process actually ran.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s