J.S.Orris
J.S.Orris

Reputation: 4821

Properly Using String Functions in Stored Procedures

I have an SSIS package that imports data into SQL Server. I have a field that I need to cut everything after and including "-". The following is my script:

SELECT LEFT(PartNumber, CHARINDEX('-', PartNumber) - 1)
FROM ExtensionBase

My question is where in my stored procedure should I use this script so that it cuts before entering data into the ExtensionBase. Can I do this in a Scalar_Value Function?

Upvotes: 0

Views: 895

Answers (1)

billinkc
billinkc

Reputation: 61239

You have two routes available to you. You can use Derived Columns and the Expressions to generate this value or use a Script Transformation. Generally speaking, reaching for a script first is not a good habit for maintainability and performance in SSIS but the other rule of thumb is that if you can't see the entire expression without scrolling, it's too much.

Dataflow

Here's a sample data flow illustrating both approaches.

enter image description here

OLE_SRC

I used a simple query to simulate your source data. I checked for empty strings, part numbers with no dashes, multiple dashes and NULL.

SELECT
    D.PartNumber
FROM
(
    VALUES
        ('ABC')
    ,   ('def-jkl')
    ,   ('mn-opq-rst')
    ,   ('')
    ,   ('Previous line missing')
    ,   (NULL)
) D(PartNumber);

DER Find dash position

I'm going to use FINDSTRING to determine the starting point. FINDSTRING is going to return a zero if the searched item doesn't exist or the input is NULL. I use the ternary operator to either return the position of the first dash, less a space to account for the dash, or the length of the source string.

(FINDSTRING(PartNumber,"-",1)) > 0 
? (FINDSTRING(PartNumber,"-",1)) - 1 
: LEN(PartNumber)

I find it helpful in these situations to first compute the positions before trying to use them later. That way if you make an error, you don't have to fix multiple formulas.

DER Trim out dash plus

The 2012 release of SSIS provided us with the LEFT function while previous editions users had to make due with SUBSTRING calls.

The LEFT expression would be

LEFT(PartNumber,StartOrdinal)

whilst the SUBSTRING is simply

SUBSTRING(PartNumber,1,StartOrdinal)

SCR use net libraries

This approach is to use the basic string capabilities of .NET to make life easier. The String.Split method is going to return an array of strings, less what we split upon. Since you only want the first thing, the zero-eth element, we will assign that to our SCRPartNumber column that is created in this task. Note that we check whether the PartNumber is null and sets the null flag on our new column.

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    if (!Row.PartNumber_IsNull)
    {
        string[] partSplit = Row.PartNumber.Split('-');
        Row.SCRPartNumber = partSplit[0];
    }
    else
    {
        Row.SCRPartNumber_IsNull = true;
    }
}

Results

You can see the results are the same however you compute them.

enter image description here

Upvotes: 6

Related Questions