Derived Column to handled mulitple conditions in CASE statement?

  • HI, I've got a case statement that works, but because I have to do it in SSIS I am at a loss:

    ---this works

    , CASE VW.LT

    when 'B1' then 'STD'

    when 'B7' then 'Q2FC'

    when 'B8' then 'Q3FC'

    when 'B9' then 'Q4FC'

    end as ValueType

    As you can see, the case statement evaluates values in one column, and depending on what they are, renames them.

    I've pulled out the Derived Column transform to accomplish but unable to find a way to handle more than one condition.

    ---this works

    ([Copy of LT] =="B9")?"STD": "NonSTD"

    --but this doesn't work

    ([Copy of LT] =="B9")?"STD": ( [Copy of LT] == "B7"?"Q2FC":( [Copy of LT]=="B8"?"Q3FC":( [Copy of LT]=="B9"?"Q4FC")

    I need to handle all conditions. How to do it in SSIS?

  • In Derived column you use following expression:

    (@[User::CopyofLT] == "B1") ? "STD" : ((@[User::CopyofLT] == "B7") ? "Q2FC" : ((@[User::CopyofLT] == "B8") ? "Q3FC" : ((@[User::CopyofLT] == "B9") ? "Q4FC" : "NonSTD")))

    I have used a variable to test the expression, you can replace it with your column name. This is working as you described for all cases.

    Vikash Kumar Singh || www.singhvikash.in

  • Vikash, thanks a lot.

    I put it in that way and Derived Column Transformation Editor 'accepted' it, meaning the Expression went black. I haven't run the SSIS package yet, but looks good so far.

    Happy Memorial Day weekend:-D

  • I have another CASE statement, similar but different, with which I am struggling.

    Here's the case statement:

    , AcctNum = CASE

    When [Obj Acct] = 0 then [Sub]

    When [Sub] = 0 then [Obj Acct]

    Else [Obj Acct] + '.' + [Sub]

    End

    FYI

    [Obj Acct] is nvarchar

    [Sub] is nvarchar

    Because now I am referring to column headings, and not values, eg. [Obj Acct] and [Sub], I think I need slightly different syntax, because the column names don't belong within quotes, but I can't get the transform to accept the way I did it (see below, remains red).

    ([Copy of Obj Acct]=="0")? [Copy of Sub] :(([Copy of Sub]=="0")?[Copy of Obj Acct]: [Copy of Obj Acct]+"."+ [Copy of Sub]))

    Can you help me convert this second CASE statement into a Derived Column transform expression? Thx

    Thanks.

  • You can use below expression in the derived column.

    [Obj Acct] == "0" ? Sub : (Sub == "0" ? [Obj Acct] : [Obj Acct] + "." + Sub)

    Vikash Kumar Singh || www.singhvikash.in

  • awesome. Thanks. I will learn this thanks to your showing me the way. I appreciate it.

  • Vikash,

    I would like the expression to evaluate without quotes around zero:

    [Obj Acct] == 0? Sub : (Sub == 0? [Obj Acct] : [Obj Acct] + "." + Sub)[/

    so that it works the way my case statement works:

    , AcctNum = CASE

    When [Obj Acct] = 0 then [Sub]

    When [Sub] = 0 then [Obj Acct]

    Else [Obj Acct] + '.' + [Sub]

    End

    With quotes the expression parses in SSIS but column values are returning with periods even when there is no value in the Sub or [Obj Acct] columns. EG. 55515. or .111. To be honest, I don't know why 0 works, because those columns don't actually have a 0 in them. They are just blank.

    However, without quotes the expression won't parse. Do you know what I can do?

  • The syntax for the derived column gets difficult to maintain doing it this way, as more lookups are added.

    An alternative is to use a lookup table - either physical or virtual (ie, generated at run time) - and add a suitable lookup to your data flow.

    The benefit of using a physical table is that you can add more lookups without having to modify the design of your package.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil, I don't get what you're saying. How would a Lookup table help me in a situation where I need to derive new values in the source table based on existing values (also in the source table)? Have you done this? If so, please provide some example.

    I have successfully implemented lookups for retrieving additional columns from referenced dataset columns (for eg. to handle joins) but what I need in the above scenario is the syntax to handle for a case statement.

    The syntax Vikash gave me works....except that it needs to evaluate 0 without quotes.

  • Sought advice from colleague.

    This worked (double quotes encasing a space)

    [Copy of Obj Acct] == " " ? [Copy of Sub] : ([Copy of Sub] == " " ? [Copy of Obj Acct] : [Copy of Obj Acct] + "." + [Copy of Sub])

    This didn't (double quotes encasing no space)

    [Copy of Obj Acct] == "" ? [Copy of Sub] : ([Copy of Sub] == "" ? [Copy of Obj Acct] : [Copy of Obj Acct] + "." + [Copy of Sub])

    Now the concatenation is working as intended, with no period if either [Sub] or [Obj Acct] column is empty.

    --I will need to evaluate why the excel column [Sub], when containing nothing is importing to SSIS as a space....

  • KoldCoffee (5/28/2013)


    Phil, I don't get what you're saying. How would a Lookup table help me in a situation where I need to derive new values in the source table based on existing values (also in the source table)? Have you done this? If so, please provide some example.

    I have successfully implemented lookups for retrieving additional columns from referenced dataset columns (for eg. to handle joins) but what I need in the above scenario is the syntax to handle for a case statement.

    The syntax Vikash gave me works....except that it needs to evaluate 0 without quotes.

    I'm sorry about that - my response was aimed at your first post, not the subsequent one, which I had assumed was a similar question, without reading it closely enough. You are correct, my idea cannot be applied in this case, though it would work for your first post.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil, you've always been spot on for me in the past, so I was :w00t: and :ermm: and :crazy:. and a little bit wondering if it had more to do with other post too, after I about half a day of it teasing the outer fringes of my brain 🙂

    Both problems solved. Lookups turns out to bring forward all the inputs plus the referenced columns and thank goodness for all the help and materials on line and here.

  • KoldCoffee (5/28/2013)


    Phil, you've always been spot on for me in the past, so I was :w00t: and :ermm: and :crazy:. and a little bit wondering if it had more to do with other post too, after I about half a day of it teasing the outer fringes of my brain 🙂

    Both problems solved. Lookups turns out to bring forward all the inputs plus the referenced columns and thank goodness for all the help and materials on line and here.

    Well done on getting it solved!

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Vikash, Phil, somebody?

    [Business Unit] is MFMGOOGLE and I want just the 'GOOGLE' part.

    This parses in the Derived Column transformation but isn't working as intended:

    SUBSTRING([Business Unit],4,LEN([Business Unit]) - 3)

    How to fix this expression to return intended result?

  • You can use following:

    RIGHT([Business Unit],6)

    Vikash Kumar Singh || www.singhvikash.in

Viewing 15 posts - 1 through 15 (of 16 total)

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