You are on page 1of 1

SSIS Expression Cheat Sheet

Problems Expression
B.I. MADE SIMPLE
http://www.pragmatic works.com
Create a file name with Expression on the Flat File or File Connection Manager:
today’s date “C:\\Project\\MyExtract”+(DT_WSTR, 30)(DT_DBDATE)
GETDATE()+“.csv”
Expression Output Example: C:\Projects\MyExtract2009-03-20.csv
Use a 2 digit date RIGHT(”0”+(DT_WSTR,2)MONTH(GETDATE()),2)
(ex. “03” for March
instead of “3”)
Multiple condition if
Expression Output: 03 (if the month is March)

In this example, the statement determines that if the ColumnName is blank


B.I. Tools and Training from
Industry Recognized Experts
statement. or NULL, it will be set to unknown. To make a Logical AND condition,
use “&&” instead of the “||” operator.
ISNULL(ColumnName) || TRIM(ColumnName) == “”? “Unknown” :
Return the first five
characters from a zip
ColumnName
Derived Column Transform in the Data Flow:
SUBSTRING(ZipCodePlus4, 1, 5)
You Trust
code
Remove a given Derived Column Transform in the Data Flow:
character from a string REPLACE(SocialSecurityNumber, “-”, “”)
(ex. Remove “-” from a
social security number)

Common SSIS Problems and Solutions


Uppercase data Derived Column Transform in the Data Flow:
UPPER(ColumnName)
Replace NULL with Derived Column Transform in the Data Flow:
another value ISNULL(ColumnName) ? “New Value” : ColumnName
Replace blanks with
NULL values
Derived Column Transform in the Data Flow:
TRIM(ColumnName) == "" ? (DT_STR, 4, 1252) NULL (DT_STR, 4, Problems Solutions
1252) : ColumnName Loop over a list of files Tasks Required: Foreach Loop, Data Flow Task
Remove any non- Script Transform in the Data Flow Task with the code as follows ( VB 2008): & load each one Solution: Configure the Foreach Loop to loop over any particular directory of files.
numeric data from a The loop should be configured to output to a given variable. Map the given variable to a
column Imports System.Text.RegularExpressions connection manager by using expressions.
Conditionally executing Solution: Double-click the precedence constraint and set the Evaluation property to
Public Overrides Sub Input0_ProcessInputRows(ByVal Row As tasks Expression and Constraint. Type the condition that you want to evaluate in the
Input0Buffer) Expression box.
If Row.ColumnName_IsNull = False Or Row.ColumnName = “” Then
Dim pattern As String = String.Empty Pass in variables when Solution: Use the /SET command in the DTExec command line or change the Property
Dim r As Regex = Nothing scheduling or running a tab in the Package Execution Utility to have the property path like:
pattern = “[^0-9]” package \Package.Variables[User::VariableName].Properties[Value]
r = New Regex(pattern, RegexOptions.Compiled) Move & rename the file at Tasks Required: File System Task
Row.ColumnName = Regex.Replace(Row.ColumnName, pattern, the same time Solution: Set the File System Task to rename the file and point to the directory you’d
“”) like to move it to. This enables you to rename and move the file in the same step.
End If Loop over an array of data Tasks Required: Execute SQL Task, Foreach Loop
End Sub in a table & perform a set of Solution: Use an Execute SQL Task to load the array and send the data into an object
Convert text to proper Script Transform with the line of partial code as follows: tasks for each row variable. Loop over the variable in a Foreach Loop by using an ADO Enumerator.
case (ex. 1st letter in Perform an incremental Tasks Required: 2 Execute SQL Tasks, Data Flow Task
each word is uppercase) Row.OutputName = StrConv(Row.InputName, VBStrConv.ProperCase) load of data Solution: Have the 1st Execute SQL Task retrieve a date from a control table of when
Build dynamic SQL Expression on the SQLStatementSource property of Execute SQL Task: the target table was last loaded and place that into a variable. In the Data Flow Task,
statement "SELECT Column FROM " + @[User::TableName] + " WHERE create a date range on your query using the variable. Then, update the control table using
DateFilterColumn = '" + (DT_WSTR,4)YEAR(@[User::DateTimeVar]) + a 2nd Execute SQL Task to specify when the table was last updated.
RIGHT("0" + (DT_WSTR,2)MONTH(@[User::DateTimeVar]), 2) + Perform a conditional Components Required: Data Flow Task, Conditional Split, Lookup Transform or Merge
RIGHT("0" + (DT_WSTR,2)DAY(@[User::DateTimeVar]), 2) + "’" update & insert Join, OLE DB Command Transform
Expression Output: SELECT Column FROM MyTable Solution: Use the Lookup Transform or Merge Join to determine if the row exists on the
destination and ignore a failed match. If the row yields blank on the key, then you know
WHERE DateFilterColumn = '20060915' the row should be inserted into target (by Condtional Split). Otherwise, the row is a
Calculate beginning of Expression on component or task: duplicate or an update. Determine if the row is an update by comparing the source value
the previous month (DT_DATE)(DT_DBDATE)DATEADD to the target value in the Conditional Split. The update can be done by an OLE DB
("dd",-1 * (DAY( GETDATE() )-1), DATEADD("month",-1, GETDATE() )) Command Transform or by loading the data into a staging table.
Round to the nearest Expression on Derived Column Transform:
two decimal mark ROUND(YourNumber, 2)
Expression Output Example: 1.2600000

CONVERT. DEVELOP. DOCUMENT. EXTEND.


TM

You might also like