{"id":1382,"date":"2012-08-08T00:00:00","date_gmt":"2012-08-08T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/ssis-basics-introducing-variables\/"},"modified":"2021-06-03T16:45:25","modified_gmt":"2021-06-03T16:45:25","slug":"ssis-basics-introducing-variables","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/bi-sql-server\/ssis-basics-introducing-variables\/","title":{"rendered":"SSIS Basics: Introducing Variables"},"content":{"rendered":"<div id=\"pretty\">\n<p class=\"start\"> \tIn previous articles in the SSIS Basics series, I showed you <a href=\"http:\/\/www.simple-talk.com\/sql\/ssis\/ssis-basics-setting-up-your-initial-package\/\">how to set up a SQL Server Integration Services<\/a> (SSIS) package. I also showed you <a href=\"http:\/\/www.simple-talk.com\/sql\/ssis\/ssis-basics-adding-data-flow-to-your-package\/\">how to add a <strong>Data Flow <\/strong>task<\/a> that contains the components necessary to extract data from a SQL Server database, transform the data, and load it into an Excel spreadsheet. <\/p>\n<p> \tIn this article, I will show you how to use variables in your SSIS package. Variables are extremely important and are widely used in an SSIS package. A variable is a named object that stores one or more values and can be referenced by various SSIS components throughout the package&#8217;s execution. You can configure a variable so its value is updated at run time, or you can assign a value to the variable when you create it. <\/p>\n<p> \tThere are two types of variables in an SSIS package: system and user-defined. SSIS automatically generates the system variables when you create your package. Components can then reference the system variables as necessary, usually for debugging and auditing purposes. You create user-defined variables as needed when you configure your package. <\/p>\n<h2>Setting Up Your SSIS Package<\/h2>\n<div class=\"note\">\n<p class=\"note\"> \tTo try out the examples in this article all you need to do is to create an Excel spreadsheet named Employee, for the purposes of this article it can be an empty file. This does follow on from the previous article in this series &#8220;<a href=\"http:\/\/www.simple-talk.com\/sql\/ssis\/ssis-basics-adding-data-flow-to-your-package\/\">SSIS Basics: Adding Data Flow to Your Package<\/a>&#8221; however it is adviseable to start from a new package. \t<\/p>\n<\/p><\/div>\n<h2>Creating Variables<\/h2>\n<p> \tIn the following sections, I will show you how to create a user-defined variable and reference it within your package. Then I will show you how to create an expression that dynamically updates the variable value at run time. Our starting point is where my previous article left off; however, all you need to do is create an Excel spreadsheet named Employee, I have saved it in the root of my d:\\ drive but you can save it wherever appropriate, once this is created you should have no problems working through these examples. <\/p>\n<h3>The Variables Window<\/h3>\n<p> \tThe easiest way to create a user-defined variable is by using the <strong>Variables<\/strong> window. When you create a variable, you specify a name, data type, and scope. <\/p>\n<p> \tThe scope controls which package elements can use the variable. You can define a variable&#8217;s scope at the package level or at the level of a specific container, task, or event handler. For example, if you configure a variable at the scope of a <strong>Foreach<\/strong><strong>Loop<\/strong> container, only that container and any tasks within the container can use the variable. However, if you configure the variable at the package level, any component within the package can reference the variable. One of the most common issues to arise when working with variables is for them to have been inadvertently created at the scope of a container or task, when they should have been created at the scope of the package. <\/p>\n<p> \tTo view the <strong>Variables<\/strong>window, right-click the design surface on the <strong>Control<\/strong><strong>Flow<\/strong> tab, and click<strong>V<\/strong><strong>ariables<\/strong>. By default, the <strong>V<\/strong><strong>ariable<\/strong><strong>s<\/strong> window appears to the left of the design surface, as shown in Figure 1. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure1-f46ef5d3-89f9-47a9-aac2-5f65817d6bf4.jpg\" alt=\"1538-Figure1-f46ef5d3-89f9-47a9-aac2-5f6\" \/><\/p>\n<p class=\"caption\"> \tFigure 1: <strong><strong>Variables<\/strong><\/strong> window in SSIS Designer <\/p>\n<p> \tAt the top of the <strong>Variables<\/strong> window, you&#8217;ll find five buttons (shown in Figure 2) and, beneath those, column headings. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure2-69996a2b-02bc-4062-a5ae-bbf78c0569be.jpg\" alt=\"1538-Figure2-69996a2b-02bc-4062-a5ae-bbf\" \/><\/p>\n<p class=\"caption\"> \tFigure 2: Buttons at the top of the <strong><strong>Variables<\/strong><\/strong> window <\/p>\n<p> \tFrom left-to-right, the buttons let you perform the following tasks: <\/p>\n<ul>\n<li>Adding a user-defined variable<\/li>\n<li>Deleting a user-defined variable (available when a variable has been created)<\/li>\n<li>Showing system variables<\/li>\n<li>Showing all variables<\/li>\n<li>Choosing the variable columns to display<\/li>\n<\/ul>\n<p> \tBy default, the <strong>Variables<\/strong> window displays the following columns: <\/p>\n<ul>\n<li><strong>Name<\/strong>: The name assigned to the variable. When you first create your variable, the value <strong>Variable<\/strong> is used. If there is already a variable named <strong>Variable<\/strong><em>,<\/em> a number is added to the name.<\/li>\n<li><strong>Scope<\/strong>: The scope at which the variable should be set. The majority of the time, you can go with a package-level scope, which means the scope should be the name of the package. To select a different scope, you must select the applicable task or container when you create your variable. It is very easy to inadvertently create a variable at an unintended scope. Before creating a variable, be certain that the correct task or container is selected or, if you want a package-level scope, that no components are selected. <\/li>\n<li><strong>Data<\/strong><strong>Type<\/strong>: The type of data that the variable can store. SSIS supports the following variable types:\n<ul>\n<li>Boolean<\/li>\n<li>Byte<\/li>\n<li>Char<\/li>\n<li>DateTime<\/li>\n<li>DBNull<\/li>\n<li>Double<\/li>\n<li>Int16<\/li>\n<li>Int32<\/li>\n<li>Object<\/li>\n<li>SByte<\/li>\n<li>String<\/li>\n<li>UInt32<\/li>\n<li>UInt64<\/li>\n<\/ul>\n<\/li>\n<li><strong>Value<\/strong>: The variable&#8217;s initial value. This can be populated here or left blank and populated at run time.<\/li>\n<\/ul>\n<p> \tIf you click the <strong>Choose Variable Columns<\/strong> icons, the <strong>Choose Variable Columns<\/strong> dialog box appears, as shown in Figure 3. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure3-7b293e93-6694-49f3-a833-d6b7340388ec.jpg\" alt=\"1538-Figure3-7b293e93-6694-49f3-a833-d6b\" \/><\/p>\n<p class=\"caption\"> \tFigure 3: Additional columns available for variables <\/p>\n<p> \tAs you can see, the <strong>Scope, Data type<\/strong>, and <strong>Value<\/strong> columns are selected by default. You can also select one of the following columns to display: <\/p>\n<ul>\n<li><strong>Namespace<\/strong><strong>: <\/strong>Shows whether the variable is a system or user-defined variable.<\/li>\n<li><strong>Raise event when variable value changes<\/strong><strong>:<\/strong>Boolean (True\/False) field that if selected will fire an <strong>OnVariableValueChanged<\/strong> event if the variable value is modified. This will be covered in more detail in a future article when looking at error handling and debugging.<\/li>\n<\/ul>\n<p> \tYou are now ready to create a variable. <\/p>\n<h3>Creating a User-Defined Variable<\/h3>\n<p> \tIn this section, I demonstrate how to create a variable and assign a value to it, which in this case, will be a file path. Using the Excel spreadsheet created in the setup, I will save a copy of the spreadsheet to a new folder using a variable to re-name it. <\/p>\n<p> \tTo create a variable, click the white space on the <strong>Control Flow<\/strong> design surface to ensure that no components are selected, and then click the <strong>Add Variable<\/strong> button in the <strong>Variables<\/strong> window. A variable is added to the first row of the window&#8217;s grid, as shown in Figure 4. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure4-e55ef1eb-7e17-4b20-b457-5398b1c738d0.jpg\" alt=\"1538-Figure4-e55ef1eb-7e17-4b20-b457-539\" \/><\/p>\n<p class=\"caption\"> \tFigure 4: A new variable in the <strong><strong>Variables<\/strong><\/strong> window <\/p>\n<p> \tNotice in Figure 4 the blue button with the black X to the left of the variable name. You can use this button to delete the variable. Also notice that the scope is set to the package name, <strong>SSISBasics<\/strong>, which is the scope we want to use for this exercise. <\/p>\n<p> \tBy default, the variable is configured with the <strong>Int32<\/strong> data type. However, for this exercise, we&#8217;ll use <strong>String<\/strong>. To change the data type, select <strong>String<\/strong> from the drop-down list in the <strong>Data Type<\/strong> column. When the data type is updated, the value in the <strong>Value<\/strong> column changes from <strong>0<\/strong> to an empty string. <\/p>\n<p> \tYou now need to enter the variable value. Simply type in the path name you plan to use for your Excel file. In this case, I used <strong>d:\\Demo<\/strong>. I also changed the variable name to <strong>DestinationFile<\/strong><strong>Path<\/strong>, as shown in Figure 5. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure5-fcde72cd-6d61-4165-8b3c-c0b63c64c2d4.jpg\" alt=\"1538-Figure5-fcde72cd-6d61-4165-8b3c-c0b\" \/><\/p>\n<p class=\"caption\"> \tFigure 5: Configuring the new variable in the <strong><strong>Variables<\/strong><\/strong> window <\/p>\n<p> \tYou&#8217;re variable should now be ready to go. Because you created it with a package scope, any component in your package can use that variable. <\/p>\n<h3>Using the Variable<\/h3>\n<p> \tI am now going to show you how to reference the new variable from within a <strong>File System<\/strong> task. First, you must add the task to the control flow, as shown in Figure 6. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure6-d4cace40-f803-40ce-9ab6-ae85b6fc7876.jpg\" alt=\"1538-Figure6-d4cace40-f803-40ce-9ab6-ae8\" \/><\/p>\n<p class=\"caption\"> \tFigure 6: Adding a File System task to the control flow <\/p>\n<p> \tThen double-click the <strong>File System<\/strong> task to open the <strong>File System<\/strong><strong> Task<\/strong><strong> Editor<\/strong>, as shown in Figure 7. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure7-b609fc62-6dba-46d1-915c-cd6817feee69.jpg\" alt=\"1538-Figure7-b609fc62-6dba-46d1-915c-cd6\" \/><\/p>\n<p class=\"caption\"> \tFigure 7: Accessing the <strong><strong>File System Task Editor<\/strong><\/strong> <\/p>\n<p> \tYou now need to configure the task&#8217;s properties. Table 1 describes how to configure each of these properties <\/p>\n<div>\n<table>\n<thead>\n<tr>\n<td>Property<\/td>\n<td>Description<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>IsDestinationPathVariable<\/strong><\/td>\n<td>Change to <strong>True<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td><strong>DestinationVariable<\/strong><\/td>\n<td>Select the variable you created earlier from the drop-down list associated with this property. When the variable is added to the property, it is preceded by the namespace name and two colons (<strong>User::<\/strong>).<\/td>\n<\/tr>\n<tr>\n<td><strong>OverwriteDestination<\/strong><\/td>\n<td>If you want to overwrite the destination every time the package runs, select <strong>True<\/strong>, otherwise leave it as <strong>False<\/strong>. However, if you select <strong>False<\/strong> and run the package more than once, SSIS will generate an error. As the name of the excel spreadsheet will not change, I suggest you select <strong>True<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td><strong>Name<\/strong><\/td>\n<td>The default name is <strong>File System Task<\/strong>. You can rename the task to better reflect what it does. For this exercise, I renamed the task <strong>Copy to new folder<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td><strong>Description<\/strong><\/td>\n<td>You can type a full description here. I used <strong>Copy the Employee&#8217;s excel spreadsheet to d:\\Demo\\<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td><strong>Operation<\/strong><\/td>\n<td>The default option is <strong>Copy File<\/strong>. Because that&#8217;s what we&#8217;ll do in this exercise, you can leave this option selected. But note that other options are available, and I will cover some of them in future articles.<\/td>\n<\/tr>\n<tr>\n<td><strong>IsSourcePathVariable<\/strong><\/td>\n<td>Leave this option set at its default value, <strong>False<\/strong>. Although we&#8217;ll be setting the destination to a variable, the source path will directly reference the original Excel file.<\/td>\n<\/tr>\n<tr>\n<td><strong>SourceConnection<\/strong><\/td>\n<td>This is where you select the connection to the source file you want to copy. From the drop-down list associated with this property, select  as currently we have no connections set up.  The <strong>Usage type:<\/strong> defaults to <strong>Existing File<\/strong> which is correct, as we have already created the spreadsheet, select the <strong>Browse<\/strong> button and browse to the Excel spreadsheet created called <strong>Employee.xlsx<\/strong> and click <strong>OK<\/strong> to accept.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p class=\"caption\"> \tTable 1: Configuring the File System task properties <\/p>\n<p> \tFigure 8 shows what your <strong>File System Task Editor<\/strong> should look like after you&#8217;ve configured the properties. Review your settings, and then click <strong>OK<\/strong>. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure8-6399c94e-9b1e-44a1-ba0e-02de9aeff2f3.jpg\" alt=\"1538-Figure8-6399c94e-9b1e-44a1-ba0e-02d\" \/><\/p>\n<p class=\"caption\"> \tFigure 8: Configuring properties in the <strong><strong>File System Task Editor<\/strong><\/strong> <\/p>\n<p> \tThe control flow should now show that the inverted red X has been removed from the <strong>File<\/strong><strong>System<\/strong> task and that the task has been renamed to <strong>Copy to new folder<\/strong>. <\/p>\n<p> \tTo verify whether the task works, run the package by clicking on the green arrow (the <strong>Run<\/strong> button) on the menu bar. Then open Windows Explorer and check that the file has been copied into the target folder correctly. <\/p>\n<h3>Using an expression to define a variable value<\/h3>\n<p> \tIn some cases, you might want to generate a variable&#8217;s value at run time, rather than assign a specific value, as we did in the previous exercise. This can be useful if the variable value needs to change during the package&#8217;s execution or that value is derived from other sources or processes. For example, the <strong>Foreach<\/strong><strong> Loop<\/strong> container might use a variable whose value must change each time the container loops through the targeted list of objects. (I&#8217;ll explain how to use a variable in this way in a future article.) <\/p>\n<p> \tIn this section, I demonstrate how to create an expression that dynamically generates a value for a <strong>String<\/strong> variable named <strong>DestinationFileName<\/strong>. The exercise uses the same test environment as the one in the previous exercise. We will again copy the original Excel file to the destination folder, only this time we&#8217;ll use an expression to define the path and file name. The expression will rename the file by tagging the date to the end of the filename, as in <strong>Employee_201265.xlsx<\/strong>. <\/p>\n<p> \tYour first step, then, is to create a variable named <strong>DestinationFileName<\/strong>, Follow the same steps you used to create the <strong>DestinationFilePath<\/strong> variable in the previous exercise, but leave the value blank, as shown in Figure 9. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure10-b9eaa6b8-4446-48ba-84d5-499c336fff87.jpg\" alt=\"1538-Figure10-b9eaa6b8-4446-48ba-84d5-49\" \/><\/p>\n<p class=\"caption\"> \tFigure 9: Creating the <strong><strong>DestinationFile<\/strong><\/strong><strong><strong>Name<\/strong><\/strong> variable <\/p>\n<p> \tTo populate the value, you&#8217;ll create an expression in the variable&#8217;s properties. An expression is a formula made up of elements such as variables, functions, and string values. The expression returns a single value that can then be used by the variable. <\/p>\n<p> \tTo create an expression for the <strong>DestinationFileName<\/strong> variable, open the <strong>Properties<\/strong> window to display the variable&#8217;s properties. You can open the window by clicking <strong>Properties <\/strong><strong>W<\/strong><strong>indow<\/strong> on the <strong>View<\/strong> menu. You can also open the <strong>Properties<\/strong> window by pressing <strong>F4<\/strong>. The <strong>Properties<\/strong> window displays the properties for whatever component is selected in your SSIS package. To display the properties for the <strong>DestinationFileName<\/strong>variable, select the variable in the <strong>Variables<\/strong> window. <\/p>\n<p> \tWhen the variable&#8217;s properties are displayed in the <strong>Properties<\/strong> window, you can create an expression that defines the variable&#8217;s value. To do so, first set the <strong>EvaluateAsExpression<\/strong> property to <strong>True<\/strong>. This enables the variable to use an expression to define its value. Then click the browse button (<strong>&#8230;<\/strong>) associated with the <strong>Expression<\/strong> property, as shown in Figure 10. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure10-5f32a733-d9c6-4af1-9623-b5e0fd2d1506.jpg\" alt=\"1538-Figure10-5f32a733-d9c6-4af1-9623-b5\" \/><\/p>\n<p class=\"caption\"> \tFigure 10: Launching the <strong><strong>Expression Builder<\/strong><\/strong> dialog box <\/p>\n<p> \tClicking the browse button opens the <strong>Expression Builder<\/strong> dialog box, which is divided into three sections, as shown in Figure 11. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure10a-29556a9c-1c87-4202-a5e2-7d801b804e9e.jpg\" alt=\"1538-Figure10a-29556a9c-1c87-4202-a5e2-7\" \/><\/p>\n<p class=\"caption\"> \tFigure 11: The <strong><strong>Expression Builder<\/strong><\/strong> dialog box <\/p>\n<p> \tNotice in Figure 11 that I&#8217;ve labeled the three sections of the <strong>Expression<\/strong><strong>Builder<\/strong> dialog box as <strong>1<\/strong>, <strong>2<\/strong>, and <strong>3<\/strong>: <\/p>\n<ol>\n<li>The variables available to use in your expression<\/li>\n<li>The functions and operators available to use in your expression<\/li>\n<li>The workspace where you create your expression<\/li>\n<\/ol>\n<p> \tThere is also a button at the bottom of the screen labeled <strong>Evaluat<\/strong><strong>e<\/strong><strong> Expression<\/strong>. When you click this button, the expression is evaluated and, if a value can be returned, it is displayed in the <strong>Evaluated value<\/strong> area just above the button. <\/p>\n<p> \tIn the previous section, we created the <strong>DestinationFilePath<\/strong> variable, which held the target folder into which we copied the Excel file. In this exercise, we&#8217;ll again copy the original Excel file to the new folder as described earlier. To do this, we&#8217;ll use the <strong>DestinationFilePath<\/strong> variable as part of our expression to provide the path name for the new <strong>DestinationFileName<\/strong> variable. <\/p>\n<p> \tAs a result, the first step you should take to create your expression is to drag the <strong>DestinationFilePath<\/strong> variable to the <strong>Expression<\/strong> section, as show in Figure 12. If you click the <strong>Evaluate Expression<\/strong> button after you add the variable, the <strong>Evaluated<\/strong><strong>value<\/strong> field should show the value assigned to that variable, which is the path <strong>d:\\Demo<\/strong>. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure11-87a45e46-0a7b-4fc6-832e-03989be4404a.jpg\" alt=\"1538-Figure11-87a45e46-0a7b-4fc6-832e-03\" \/><\/p>\n<p class=\"caption\"> \tFigure 12: Adding the <strong><strong>DestinationFilePath<\/strong><\/strong> variable to your expression <\/p>\n<p> \tNext, you&#8217;ll need to add to your expression the elements necessary to name the file. First, add a concatenation operator (<strong>+<\/strong>), and then add the string value <strong>Employee<\/strong>, enclosed in double quotes, as shown in the following script: <\/p>\n<pre>\t@[User::DestinationFilePath] + \"Employee\" <\/pre>\n<p> \tYou can evaluate your expression at any time by clicking on the <strong>Evaluate<\/strong><strong>Expression<\/strong> button. For instance, if you evaluate your expression at this point, you should receive the following results: <\/p>\n<pre>\td\\Demo\\Employee <\/pre>\n<p> \tI will now show you how to include the date and time in the expression so that the variable can include them. To create this part of the expression, we&#8217;ll use the following functions: <\/p>\n<ul>\n<li><strong>GETDATE:<\/strong> Returns the current date and time.<\/li>\n<li><strong>YEAR:<\/strong> Returns the year component (as an integer) of a date value.<\/li>\n<li><strong>MONTH:<\/strong> Returns the month component (as an integer) of a date value.<\/li>\n<li><strong>DAY:<\/strong> Returns the day component (as an integer) of a date value.<\/li>\n<li><strong>RIGHT:<\/strong> Returns the number of characters specified counting from the right of the string.<\/li>\n<\/ul>\n<p> \tThe <strong>GETDATE<\/strong> function returns data as a <strong>DT_DBTIMESTAMP<\/strong> value, which is made up of 29 characters that include integers, colons, dashes, a space, and a period. Consequently, using this function in conjunction with the other functions makes it easier to concatenate the values necessary to create the file name. <\/p>\n<div class=\"note\">\n<p class=\"note\"> \t<strong>Note:<\/strong><br \/> \tThere are many ways to build the expression I am about to explain, but this is the method I use and find easiest. \t<\/p>\n<\/p><\/div>\n<p> \tWhen building the expression, we need to add the date parts one at a time &#8211; year, month, date, hours, and minutes. <\/p>\n<p> \tAfter we add each component, we can use the <strong>Evaluate<\/strong><strong>Expression<\/strong> button to see what the value looks like to ensure that we&#8217;re happy with it up to that point. <\/p>\n<p> \tBecause we want the year to appear before the other data parts, we&#8217;ll start with that element of the expression. To add the year, we&#8217;ll use the <strong>YEAR<\/strong> function along with the <strong>GETDATE<\/strong> function. This will return a four-digit integer, which we&#8217;ll convert to a string. <\/p>\n<p> \tAs noted above, the <strong>GETDATE<\/strong> function returns the current timestamp. But we can apply the <strong>YEAR <\/strong>function to that timestamp to extract only the year value, as shown in the following code fragment: <\/p>\n<pre>\tYEAR(GETDATE())  <\/pre>\n<p> \tThis will return only the year, but as an integer. However, because we will be concatenating that value with a string value, which need to convert the year to a string, as the following code shows: <\/p>\n<pre>\t(DT_WSTR,4)YEAR(GETDATE())  <\/pre>\n<p> \tNotice that, to convert the year value to a string, we must precede the <strong>YEAR<\/strong> function with the target data type and length in parentheses. We can then append this code with our original code, as shown in the following script: <\/p>\n<pre>\t@[User::DestinationFilePath] + \"Employee\"\n+ (DT_WSTR,4)YEAR(GETDATE())  <\/pre>\n<p> \tNow if we use the <strong>Evaluate Expression<\/strong> button, the results should look like the following: <\/p>\n<pre>\td:\\Demo\\Employee2012 <\/pre>\n<p> \tNext, we need to add the month to our expression. We will use the <strong>MONTH<\/strong> function with the <strong>GETDATE<\/strong> function in similar to how we used the <strong>YEAR<\/strong> function above. However, we must take extra steps to accommodate the fact that the <strong>MONTH<\/strong> function returns a single digit for months prior to October and returns two digits from the other months. When the function returns a single digit, we need to precede the returned value with a <strong>0<\/strong> to ensure we always return two characters. That way, dates such as 15 June 2012 will be returned as 20120615, rather than 2012615, which makes it easier to manage files in such applications as Windows Explorer. <\/p>\n<p> \tThe trick in doing this is to add the <strong>0<\/strong> only when we need it. That&#8217;s where the <strong>RIGHT<\/strong> function comes in. The function takes two arguments. The first argument is an expression that returns a character value. The second argument is the number of characters we want to extract from that value. However, those characters are extracted from right-to-left. <\/p>\n<p> \tLet&#8217;s look at a couple examples of the <strong>RIGHT<\/strong> function to demonstrate how it works. The first one includes a simple expression that concatenates <strong>0<\/strong> and <strong>6<\/strong>: <\/p>\n<pre>\tRIGHT(\"0\"+\"6\",2) <\/pre>\n<p> \tThe expression comprises everything before the comma. In this case, it concatenates the two values (rather than adding them) and returns the value <strong>06<\/strong>. The second argument, <strong>2<\/strong>, specifies that only the right two characters be returned by the function. Because the expression returned only two characters, the function will return both of them. <\/p>\n<p> \tHowever, suppose your expression returns more than two characters. In the following example, a single digit is concatenated with two digits: <\/p>\n<pre>\tRIGHT(\"0\"+\"12\",2) <\/pre>\n<p> \tThe expression in this case returns the value <strong>012<\/strong>. However, the second argument specifies that the <strong>RIGHT<\/strong> function should return only the right two characters, so the function will return only <strong>12<\/strong>. <\/p>\n<p> \tNow let&#8217;s return to the expression we&#8217;re creating to generate the file name. At this point, we&#8217;re trying to add the month components. First, we need to extract the month and convert it to a string value, just like we did with the year: <\/p>\n<pre>\t(DT_WSTR, 2) MONTH(GETDATE()) <\/pre>\n<p> \tAs you would expect, this part of the expression will return a one-digit or two-digit integer that represents the month. We can then use this code within the <strong>RIGHT<\/strong> function to ensure that we always extract two digits: <\/p>\n<pre>\tRIGHT(\"0\"+(DT_WSTR, 2) MONTH(GETDATE()) ,2) <\/pre>\n<p> \tNotice that the first argument is an expression that concatenates a <strong>0<\/strong> with the month returned by the other part of the formula. That means the expression will also return a two or three characters, depending on the month. However, because <strong>2<\/strong> is specified as the second argument, the <strong>RIGHT<\/strong> function will return only the right two characters, thus ensuring that the outer expression always includes two characters for the month. <\/p>\n<p> \tWe can then concatenate this code with our original expression, as shown in the following script: <\/p>\n<pre>\t@[User::DestinationFilePath] + \"Employee\"\n+ (DT_WSTR,4)YEAR(GETDATE())\n+ RIGHT(\"0\"+(DT_WSTR, 2) MONTH(GETDATE()) ,2) <\/pre>\n<p> \tAfter the month is added to our outer expression, we use the <strong>Evaluate Expression<\/strong> button to view the current value.. The results should look similar to the following: <\/p>\n<pre>\td:\\Demo\\Employee201207 <\/pre>\n<p> \tNext, we will add the day to our expression. Adding the day is similar to what we did to add the month, except that we use the <strong>DAY<\/strong> function, as shown in the following code: <\/p>\n<pre>\t@[User::DestinationFilePath] + \"Employee\"\n+ (DT_WSTR,4)YEAR(GETDATE())\n+ RIGHT(\"0\"+(DT_WSTR, 2) MONTH(GETDATE()) ,2)\n+ RIGHT(\"0\"+(DT_WSTR, 2) DAY(GETDATE()) ,2) <\/pre>\n<p> \tAs you can see, we&#8217;ve concatenated the day information with the rest of our expression. When you click the <strong>Evaluate Expression<\/strong> button, it should now return results similar to the following: <\/p>\n<pre>\td:\\Demo\\Employee20120715 <\/pre>\n<p> \tAll that&#8217;s left to do is to add the Excel file extension. Simply add another concatenate operator, followed by <strong>.XLSX<\/strong> enclosed in double quotes. Our full expression should now be complete: <\/p>\n<pre>\t@[User::DestinationFilePath] + \"Employee\"\n+ (DT_WSTR,4)YEAR(GETDATE())\n+ RIGHT(\"0\"+(DT_WSTR, 2) MONTH(GETDATE()) ,2)\n+ RIGHT(\"0\"+(DT_WSTR, 2) DAY(GETDATE()) ,2)\n+ \".XLSX\" <\/pre>\n<p> \tIf we click the <strong>Evaluate Expression<\/strong> button one more time, we should see the results we&#8217;ve been looking for: <\/p>\n<pre>\td:\\Demo\\Employee20120623.XLSX <\/pre>\n<p> \tWe&#8217;ve built this expression up in stages so you could better see how all the pieces fit together. The expression, as it appears in the <strong>Expression Building<\/strong> dialog box on your system, should now look similar to the one shown in Figure 13. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure13-2d1b4801-1302-4786-8e08-32a1dd0e54bb.jpg\" alt=\"1538-Figure13-2d1b4801-1302-4786-8e08-32\" \/><\/p>\n<p class=\"caption\"> \tFigure 13: Your final expression in the <strong><strong>Expression Builder<\/strong><\/strong> dialog box <\/p>\n<p> \tAfter you&#8217;ve evaluated your expression, click <strong>OK<\/strong> to close the <strong>Expression<\/strong><strong>Build<\/strong><strong>er<\/strong> dialog box. The <strong>V<\/strong><strong>ariables<\/strong> window should now show the value of the <strong>DestinationFileName<\/strong> variable as the one generated by the expression, as shown in Figure 14. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure14-4a7c9cef-cb3a-4c15-98f3-7fe46c8cff8c.jpg\" alt=\"1538-Figure14-4a7c9cef-cb3a-4c15-98f3-7f\" \/><\/p>\n<p class=\"caption\"> \tFigure 14: The new value for the <strong><strong>DestinationFileName<\/strong><\/strong> variable <\/p>\n<p> \tYour final step is to update the <strong>File System <\/strong>task so it uses the <strong>DestinationFileName<\/strong> variable. To do this, open the <strong>File System Task Editor<\/strong> for the task named <strong>Copy to new folder<\/strong>. Change the value of the <strong>DestinationVariable<\/strong> property to the <strong>DestinationFileName<\/strong> variable, as shown in Figure 15. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure15-16ceebcd-00af-4b70-b1a0-d25f036de496.jpg\" alt=\"1538-Figure15-16ceebcd-00af-4b70-b1a0-d2\" \/><\/p>\n<p class=\"caption\"> \tFigure 15: Using the <strong><strong>DestinationFileName<\/strong><\/strong> variable <\/p>\n<p> \tClick <strong>OK<\/strong> to close the <strong>File System Task Editor<\/strong>. Then check that the new variable works correctly by running the package. When you&#8217;re finished, open Windows Explorer and verify that the new file has been created and that it uses the correct name. <\/p>\n<h2>System Variables<\/h2>\n<p> \tEach SSIS package includes a large number of system variables that you can use for debugging, error handling, change tracking, and other purposes. Figure 16 shows a list of system variables in our package. <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1538-Figure9-46c81a0e-cd46-4617-8bd6-30d63967b258.jpg\" alt=\"1538-Figure9-46c81a0e-cd46-4617-8bd6-30d\" \/><\/p>\n<p class=\"caption\"> \tFigure 16: System variables in our SSIS package <\/p>\n<p> \tLike user-defined variables, system variables are scope-specific. In other words, they are either related to a task, container, or package. The top red block in Figure 16 are system variables linked to the <strong>Data<\/strong><strong>Flow<\/strong> task, and the bottom block are those related to the package as a whole. The variable in the blue box is the user-defined variable <strong>DestinationFilePath<\/strong>. <\/p>\n<h2>Summary<\/h2>\n<p> \tIn this article, we created two variables. For the first variable, we assigned a static value, which is used by the variable whenever it is referenced during package execution. For the second variable, we used an expression to define the value. This way, the variable&#8217;s value is generated automatically at run time. The article also demonstrated how to use the <strong>Copy File<\/strong> function in <strong>File System<\/strong> task to copy a file and rename it. We have also looked briefly at system variables. In future articles, I will show you how to use these for error handling and debugging and how to work with user-defined variables when setting up your package&#8217;s configuration and deployment. I will also show you how to use variables more extensively. <\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the third of her SSIS Basics articles, Annette Allen shows you how to use Variables in your SSIS Packages, and explains the functions of the system-defined variables.&hellip;<\/p>\n","protected":false},"author":10747,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143528],"tags":[4242,5664,4150,4306,5698],"coauthors":[],"class_list":["post-1382","post","type-post","status-publish","format-standard","hentry","category-bi-sql-server","tag-basics","tag-data-flow","tag-sql","tag-ssis","tag-variables"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/10747"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=1382"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1382\/revisions"}],"predecessor-version":[{"id":91227,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1382\/revisions\/91227"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=1382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=1382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=1382"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=1382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}