dinsdag 4 september 2007

SSIS - Script functions trim leading zeros

In some development case, you may have to trim leading zero's. In SQL, it is possible with the SQL function PATINDEX. But how to do it within a SSIS package? The Data Flow Transaformation task "Derived column" doesn't provide any function in order to realise this operation. That's whi I developed a little script that determine the position of the first character that doesn't match a give character; in this case a zero.

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
'Declare variable
Dim StartPosition As Integer

StartPosition = DefineFirstPositionChar(Row.ColumnToBeSearched, "0")

Row.ColumnToBeSearched = Mid(Row.ColumnToBeSearched, StartPosition, Row.ColumnToBeSearched.Length)
End Sub

Function DefineFirstPositionChar(ByVal EntityID As String, ByVal SearchChar As String) As Integer
'Declare variable
Dim Length As Integer
Dim letter As String
Dim start As Integer = 1

'Store the length of the entity
Length = EntityID.Length

'Loop through the string of the entity
'When letter SearchChar is found return the start position through DefineFirstPositionChar
Do Until start = Length
letter = Mid(EntityID, start, 1)
If letter <> SearchChar Then
DefineFirstPositionChar = start
Exit Do
End If
start = start + 1
Loop

Exit Function
End Function