Whats wrong with this concatenation?

  • I have this SSIS expression and it is evaluated as below with the default values replacing the SSIS variables. This is the raw expression:

    "Select "+(DT_STR, 20, 1252)@[User::BatchID]+ " as BatchID, '" +@[User::BatchDateISO]+ "' as BatchDateISO, 'FH' as RecordType, 'AS400' as SourceSystem, CASE WHEN LEN("+(DT_STR, 10, 1252)@[User::FileLoadNumber]+") = CAST(1 as VARCHAR) THEN '0'+'"+ (DT_STR, 10, 1252) @[User::FileLoadNumber]+"'+'"+@[User::BatchDateISO]+"' ELSE '"+ (DT_STR, 10, 1252) @[User::FileLoadNumber]+"'+'"+@[User::BatchDateISO]+"' END as FileLoadNumber"

    It is evaluated as:

    Select 9 as BatchID, '20121017' as BatchDateISO, 'FH' as RecordType, 'AS400' as SourceSystem, CASE WHEN LEN(9) = CAST(1 as VARCHAR) THEN '0'+'9'+'20121017' ELSE '9'+'20121017' END as FileLoadNumber

    This is returning the result without appending 0, which means always the ELSE part in CASE is chosen for FileLoadNumber column value.

    If the BatchID value is of single digit it has to concatenate with BatchDateISO by appending 0 in front of BatchID

    If BatchID is 9, then the result should be 0920121017 for FileLoadNumber column

    If it is 19, then the result should be 1920121017 for FileLoadNumber column

    Thanks a lot!

  • Hi Learner,

    A suggestion and a question. The suggestion. It's very helpful if you do not cross-post your questions. That keeps the responses in one place. Folks will find the question wherever you put it.

    The question. I'm confused about what the issue is. Is the expression not giving the expected output or is the output not giving the expected query results?

  • Like this?

    "Select "+(DT_STR, 20, 1252)@[User::BatchID] + " as BatchID, '" +@[User::BatchDateISO] + "' as BatchDateISO, 'FH' as RecordType, 'AS400' as SourceSystem, " +RIGHT("00" + (DT_STR, 10, 1252)@[User::BatchID],2) + @[User::BatchDateISO] + " as FileLoadNumber"

    Your expression references FileLoadNumber variable, which you did not say what its value is or where it fits in, But why are you doing that crazy case?

    Simple RIGHT("00" + (DT_STR, 10, 1252)@[User::BatchID],2) will Create a string 009, and then take the Right 2 characters, ie 09. if the batch id is 19 then it will create a string 0019 and then take the Right 2 chars getting 19.

  • I would dump the expression and use Execute SQL Task as it is easier to read and maintain.

    Here is what I would do, you might need to tweak as I took 10 minutes just to put this together:

    /* comment out declares and set in sql task

    set-up ado.net connection

    pass in variables that are declared here

    Set-up result set to single row

    For result set, set result name to 0, use user variable FileLoadNumber as variable name

    DECLARE @BatchID INT

    DECLARE @FileLoadNumber int

    DECLARE @BatchDateISO datetime

    SET @BatchID = 11

    SET @BatchDateISO = GETDATE()

    SET @FileLoadNumber = '9'

    */

    Select CAST(@BatchID AS VARCHAR(20))

    + 'FHAS400'

    + CASE WHEN LEN(@FileLoadNumber) = 0 then '0' + convert(char(8),@BatchDateISO,112)

    ELSE CAST(@FileLoadNumber AS VARCHAR(10)) + convert(char(8),@BatchDateISO,112) end

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply