BlueEyes
BlueEyes

Reputation: 337

Importing data from Excel in SSIS, empty values

I'm using SSIS to import data from Excel sheets into SQL Server 2005, the data contains some columns with data that should have the value of the previous non-empty cell in the same column, it looks like this:

2005 | 15 | Something1
     | 12 | Something2
     | 14 | Something3
2006 | 10 | Something4
     |  2 | Something5
     | 18 | Something6
     | 15 | Something7

It should fill-in 2005 and 2006, but all I could get is nulls. Can I put this field's value in a variable if it's not null, and use it to replace the null values that come after it?

Upvotes: 2

Views: 3168

Answers (1)

Eric
Eric

Reputation: 95113

You'll have to use a Script Component in the Data Flow and go through each row, using a variable to hold the last known value.

Update: Here's the code. I did this in VB in SSIS 2008, so there may be some editing you have to do (for example, you may have to use Dts.Variables["User::LastYear"].Value.ToInt32() instead of Variables.LastYear--I can't remember off the top of my head):

Dim LastYear As Integer

Public Overrides Sub PreExecute()
    MyBase.PreExecute()
    LastYear = 0
End Sub

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
    If Row.year_IsNull Then
        Row.year = LastYear
    Else
        LastYear = Row.year
    End If
End Sub

Hopefully, this helps!

Upvotes: 3

Related Questions